Newsgroups: comp.unix.questions
Path: utzoo!utgpu!news-server.csri.toronto.edu!rpi!zaphod.mps.ohio-state.edu!van-bc!cynic!curt
From: curt@cynic.wimsey.bc.ca (Curt Sampson)
Subject: Re: Delimeters when using the "cut" tool.
Organization: Mad Artists' Technological Hangout
Distribution: na
Date: Sun, 7 Apr 1991 06:49:58 GMT
Message-ID: <1991Apr7.064958.26710@cynic.wimsey.bc.ca>
References: <T2Q_XF#@ee.eng.ohio-state.edu> <1991Apr5.144839.28412@dg-rtp.dg.com>

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

> In article <T2Q_XF#@ee.eng.ohio-state.edu>,
> juodvalk@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@cynic.uucp         | all systems in the network is not known anywhere."
curt@cynic.wimsey.bc.ca |    --Berkeley Mail Reference Manual (Kurt Schoens)
