[plug] Using the "cut" command on fields seperated with variable number of spaces

Russell Steicke r.steicke at bom.gov.au
Thu Jan 30 09:12:34 WST 2003


On Thu, Jan 30, 2003 at 08:59:27AM +0800, David Buddrige wrote:
...
> ps -aux 
> 
> I get the following output. 
> 
> USER       PID %CPU %MEM   VSZ  RSS TTY      STAT START   TIME COMMAND
> root         1  0.0  0.0  1368   76 ?        S     2002   0:04 init [5]
...
> 
> In the past, when I have tried to extract a single field from a list, I
> have used the cut command to specify the appropriate field, however in this
> instance, there is no delimeter between the fields, other than a variable
> number of spaces.  Does anyone know how to extract (for example) the PID 
> field from this kind of output? 

awk by default sees fields separated by any amount of white space, so:

  ps aux | tail +2 | awk '{print $2}'

tail +2 gets rid of the header line from ps.  Or:

  ps aux | awk '{ if ($0 !~ /^USER/) print $2}'

$0 is the whole line.

awk's great for this type of thing.  You can do a lot more with it than
that, there's even a story somewhere about people submitting AI
assignments written in awk.





-- 
Russell Steicke

-- Fortune says:
Be assured that a walk through the ocean of most Souls would scarcely
get your Feet wet.  Fall not in Love, therefore: it will stick to your face.
		-- National Lampoon, "Deteriorata"



More information about the plug mailing list