getting a field from a line in awk/sed/?

Tom Christiansen tchrist at convex.COM
Thu Jan 10 04:43:26 AEST 1991


>From the keyboard of snk at bae.bae.bellcore.com (Samuel N Kamens):
:The question I had was, how can I use UNIX tools to 
:get one field out of a line?
:
:The input will look like this:
:
:Rank   Owner      Job  Files                                 Total Size
:1st    fort       24   (standard input)                      40354 bytes
:3rd    root       178  standard input                        57 bytes
:4th    mike       25   (standard input)                      26895 bytes
:6th    rdg        686  (standard input)                      733 bytes
:7th    bdt        688  (standard input)                      31053 bytes
:
:And the output should look like this:
:
:Owner
:fort 
:root 
:mike 
:rdg  
:bdt  
:
:Any ideas?

( Isn't there a local guru you could ask this of? )

Anyway, there are innumerable ways to do this, of which 
the most straightforward (and to my mind best for 
the task you described) is probably using awk:

    awk '{print $2}'

You could also use sed or perl:

    sed -e 's/^\([^ 	]*\)[ 	]*\([^ 	]*\).*/\2/'
    # but be careful of leading white space

    perl -an 'print "$F[1]\n"' 
    # perl arrays are 0-based like C 

    perl -n '/^\s*\S+\s+(\S+)/ && print "$2\n";'


--tom



More information about the Comp.unix.questions mailing list