Click to See Complete Forum and Search --> : cut command
Architect
05-23-2005, 08:09 AM
I tried cutting this output to get only the last column but I keep getting different output depending on the length of the line.
abcdef pts/7 May 23 11:27 (zab2h0pr)
another pts/8 May 23 03:42 (zab2h0q2)
How do i do this?
The command i use is this.
who | cut -f 10 -d' '
WakeBdr
05-23-2005, 02:26 PM
Try this:
who|sed -e 's/.* (/(/'
This will replace everything on the line up to the " (" with a "(", essentially giving you just the last column.
bwkaz
05-23-2005, 09:54 PM
When you use -d' ', cut treats each and every space as a field separator, even if you have a bunch of spaces next to each other. (Tabs aren't counted as spaces, either.)
For example, with this line:
a b e this cut command:
cut -d' ' -f3 will print nothing. Using -f5 will print "e".
You could specify ( as your field separator, and then pipe through another cut with ) as the field separator. Or, you could use awk instead. (Its default field separator is "any string of whitespace".)
Architect
05-24-2005, 07:02 AM
@bwkaz: Thanks, right now I am using the '(' as the separator.
I should try the awk thing though. Any hints on how to do it?
bwkaz
05-24-2005, 07:07 PM
who | awk '{print $6;}'
should work, based on the output you gave.