Server Fault is a question and answer site for system and network administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I try to extract the hostname from the following string (created using who am i):

username pts/0        2010-10-05 17:30 (

I need a solution which also works if ( is not present which is the case when I am logged in locally. Furthermore the solution also has to work on a Mac where who am i returns the following string:

username ttys006      Oct  5 16:47 (

The problem is that the mac puts spaces in the date which makes it hard to use cut. I think, there must be a solution using sed to extract the substring, but I have problems figuring out how to use sed appropriately.

Update: Mad's approach

This approach only works well if there are actually parentheses present. If not, the following happens:

$ who am i | cut -f2 -d '(' | tr -d ')'
username ttys006      Oct  5 16:47
share|improve this question
up vote 15 down vote accepted

This is where awk is the best tool. awk -F"[()]" '{print $2}' should pull out what's in parenthesis. Prints nothing if it's not there. So...

$ who am i | awk -F"[()]" '{print $2}'

--Christopher Karel

share|improve this answer
Thanks a lot. You saved my day. – t6d Oct 5 '10 at 15:26
Do you really need a signature in your answer? – Chris Oct 5 '10 at 19:04
@Chris Why not? I put them on E-Mails, forum posts, etc. It's a habit, and the space usage is negligible. – Christopher Karel Oct 5 '10 at 19:57
Do you put your signature/tagline twice in forums/blogs/emails/etc. See specifically, "Can I use a signature or tagline?" – Chris Oct 7 '10 at 17:36
As a matter of fact, Chris, yes. All those methods already have user information included in the post via account details/source E-Mail address/etc. Consider it a personal touch analogous to signing a document, despite the letter head and envelope containing far more information about yourself. The FAQ entry is new to me, and duly noted. But I'll probably keep signing my posts, until a significant number of people complain. – Christopher Karel Oct 7 '10 at 21:02

You can use who am i | grep -o -e "\((.*)\)" on both systems, and it simply returns an empty string if you're logged in locally.

share|improve this answer
Another pretty neat solution, I have to say. Thanks. :) – t6d Oct 5 '10 at 15:27

Here's a sed version which works with or without parentheses around the hostname or spaces in the date:

who am i | sed 's/.* (\?\([^)]*\))\?$/\1/'
share|improve this answer

If you like cut (and I'm not saying there aren't more elegant ways to do it, but you chose the tool, and I rather like cut, too), try

who am i | cut -f2 -d\( | tr -d ')'

You take the second field, using left-paren as the separator (whcih will include the closing paren, though not the opening one), and then use tr to delete the close-paren.

I'm not sure about the case when the string isn't present, as all the times I try it on a locally logged-on machine I get something returned in the brackets. Could you give an example of what you get under those circumstances?

share|improve this answer
Thanks for trying. This was one of my first attempts, too. It doesn't work though (see updated question). – t6d Oct 5 '10 at 15:08

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.