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