3

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

username pts/0        2010-10-05 17:30 (www.example.com)

I need a solution which also works if (www.example.com) 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 (www.example.com)

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
0

4 Answers 4

15

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}'
www.example.com


--Christopher Karel

4
  • 1
    Do you really need a signature in your answer?
    – Chris
    Oct 5, 2010 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. Oct 5, 2010 at 19:57
  • Do you put your signature/tagline twice in forums/blogs/emails/etc. See serverfault.com/faq specifically, "Can I use a signature or tagline?"
    – Chris
    Oct 7, 2010 at 17:36
  • 1
    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. Oct 7, 2010 at 21:02
5

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.

1
  • Another pretty neat solution, I have to say. Thanks. :)
    – t6d
    Oct 5, 2010 at 15:27
2

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?

1
  • Thanks for trying. This was one of my first attempts, too. It doesn't work though (see updated question).
    – t6d
    Oct 5, 2010 at 15:08
2

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/'

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .