[comp.unix.questions] Delimeters when using the "cut" tool.

juodvalk@ee.eng.ohio-state.edu (Vincent Vladas Juodvalkis) (04/05/91)

I am trying to write a program in sh that will check a user's quota
before allowing them to use the compress tool.  I have the program
writen and am now in the process of finding the thousand or so errors
that I always manage to code into a fifty line program.

Anyway, I am relatively new to shell programming and am having
dificulty with the cut tool.  What I need to use it for is to get one
integer out of the output I get from the command  "quota -v".  The
output from this command is inconsistant between users so I cannot
just use the collumn feature of cut.  The output from "quota -v" looks
like this...

Disk quotas for juodvalk (uid 2360):
Filesystem     usage  quota  limit    timeleft  files  quota  limit    timeleft
/tmp_mnt/homea
                 972  50000  50500                 33      0      0


I use the line 

`quota -v 2>&1 | grep -v quota | grep -v /`

to get 

                 972  50000  50500                 33      0      0


Now my problem comes when I try to cut this line.  If I just try to
cut it by filed, trying to get the second field (50000), using the
following

quota -v 2>&1 | grep -v quota | grep -v / | cut -f2

the output I get is 

                 972  50000  50500                 33      0      0

Which is the same as before.

The reason that I came up with for this is that the generic delimeter
is a tab, and this seemed to imply that the spaces in the output were
not tabs.  So I tried using spaces as the delimeter.  The code I osed
looked like

quota -v 2>&1 | grep -v quota | grep -v / | cut -d" " -f2

the output I got was just a bank line.  My hypothesis for this is that
the cut cuts after the first space it encounters and returns all that
ir finds until the next space it encounters (which in this case
happens to be the very next character, thus returning a line with
nothing in it).

What I need is either a was to get rid of all the extra spaces, or
maybe just a better way to cut up the lines.


I would appreciate any suggestion.



	Vincent



-------------------------------------------------------------------------------
    Vincent V. Juodvalkis                           "I can still move...
    juodvalk@ee.eng.ohio-state.edu                   I can still drink."
    juodvalk@cis.ohio-state.edu                            -Robert Reed
    The Ohio State University

hunt@dg-rtp.rtp.dg.com (Greg Hunt) (04/05/91)

In article <T2Q_XF#@ee.eng.ohio-state.edu>, juodvalk@ee.eng.ohio-state.edu (Vincent Vladas Juodvalkis) writes:
> 
> ...
> Now my problem comes when I try to cut this line.  If I just try to
> cut it by filed, trying to get the second field (50000), using the
> following
> 
> quota -v 2>&1 | grep -v quota | grep -v / | cut -f2
> 
> the output I get is 
> 
>                  972  50000  50500                 33      0      0
> 
> Which is the same as before.
> 
> The reason that I came up with for this is that the generic delimeter
> is a tab, and this seemed to imply that the spaces in the output were
> not tabs.

That's right.  Cut's default delimiter is a tab, and that's part of
the problem.  But ...

> So I tried using spaces as the delimeter.  The code I osed
> looked like
> 
> quota -v 2>&1 | grep -v quota | grep -v / | cut -d" " -f2
> 
> the output I got was just a bank line.  My hypothesis for this is that
> the cut cuts after the first space it encounters and returns all that
> ir finds until the next space it encounters (which in this case
> happens to be the very next character, thus returning a line with
> nothing in it).

Yup.  You figured it out right.

> What I need is either a was to get rid of all the extra spaces, or
> maybe just a better way to cut up the lines.
> 
> I would appreciate any suggestion.

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}'

Awk ignores the multiple spaces, and this should print out 50000,
which is what you're looking for.  The $2 refers to the second field
in the input line.

You can do a lot more with awk than you can with cut, so when cut
doesn't cut it (oooooh - that was a baaaaad one), try awk.  The man
page for awk (at least on my system) is pretty good, so take a look
at it and see what else you can do with it.  Enjoy!

-- 
Greg Hunt                        Internet: hunt@dg-rtp.rtp.dg.com
DG/UX Kernel Development         UUCP:     {world}!mcnc!rti!dg-rtp!hunt
Data General Corporation
Research Triangle Park, NC, USA  These opinions are mine, not DG's.

curt@cynic.wimsey.bc.ca (Curt Sampson) (04/07/91)

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)