Delimeters when using the "cut" tool.

Curt Sampson curt at cynic.wimsey.bc.ca
Sun Apr 7 16:49:58 AEST 1991


In article <1991Apr5.144839.28412 at dg-rtp.dg.com>
hunt at dg-rtp.rtp.dg.com writes:

> In article <T2Q_XF#@ee.eng.ohio-state.edu>,
> juodvalk at ee.eng.ohio-state.edu (Vincent Vladas Juodvalkis) writes:
>
> > quota -v 2>&1 | grep -v quota | grep -v / | cut -d" " -f2
>
> I think it's time for you to learn about a more powerful tool for
> this sort of thing.  Cut works great for simple cases, but when it
> gets to be a pain, I switch to using awk.  Try using this command
> line:
> 
>     quota -v 2>&1 | <all the grep stuff> | awk '{print $2}'

You can get awk to take care of all of the "grep stuff," too.  In this
case, he wanted to get the second field of the fourth line.  Thus, you
just have to tell awk to run its command on the fourth record (line)
only:

	quota -v 2>&1 | awk 'NR == 4 { print $2 }'

This is a faster and simpler way of doing it.

You can also do the pattern matching with awk, if you like.  Awk will
do things like:

	quota -v 2>&1 | awk '$0 !~ /quota/ && $0 !~ /\// { print $2 }'

to do the same search that the two grep commands did.

I agree that awk is well worth learning.  It easily doubles the power
available from shell scripts, and is not terribly difficult.  Anybody
should be able to write simple awk programs in a couple of hours if
they've got a decent book (or chapter) on it.

cjs
-- 
                        | "It is actually a feature of UUCP that the map of
curt at cynic.uucp         | all systems in the network is not known anywhere."
curt at cynic.wimsey.bc.ca |    --Berkeley Mail Reference Manual (Kurt Schoens)



More information about the Comp.unix.questions mailing list