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
t6d
  • 507
  • 2
  • 5
  • 12

4 Answers4

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

Christopher Karel
  • 6,442
  • 1
  • 26
  • 34
  • 1
    Do you really need a signature in your answer? – Chris Oct 05 '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 05 '10 at 19:57
  • Do you put your signature/tagline twice in forums/blogs/emails/etc. See http://serverfault.com/faq specifically, "Can I use a signature or tagline?" – Chris Oct 07 '10 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. – Christopher Karel Oct 07 '10 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.

Darth Android
  • 545
  • 5
  • 12
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?

MadHatter
  • 78,442
  • 20
  • 178
  • 229
  • Thanks for trying. This was one of my first attempts, too. It doesn't work though (see updated question). – t6d Oct 05 '10 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/'
Dennis Williamson
  • 60,515
  • 14
  • 113
  • 148