[comp.unix.questions] getting a field from a line in awk/sed/?

snk@bae.bae.bellcore.com (Samuel N Kamens) (01/10/91)

I'm sorry if anybody already got this -- I posted it
yesterday, and it doesn't seem to have made it off
the machine I posted it on.

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?


-------------------------------------------------------------------------------
Sam Kamens					Bell Communications Research
snk@cellar.bae.bellcore.com			Phone: (908) 699-7381
444 Hoes Lane  Room RRC 1C-205
Piscataway, NJ 08854

tchrist@convex.COM (Tom Christiansen) (01/10/91)

From the keyboard of snk@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

dberg@informix.com (David I. Berg) (01/10/91)

In article <1991Jan9.162817.17038@porthos.cc.bellcore.com> snk@bae.bae.bellcore.com (Samuel N Kamens) writes:
>
>The question I had was, how can I use UNIX tools to 
>get one field out of a line?

Use cut. The syntax is ` cut -fn -d" " ', where n is the number of the
field you wish to extract. -d" " specifies spaces as the delimiter between
fields. `man cut' will give you all the particulars.
  ___                   ___              dberg@cougar.informix.com
  /  ) __      . __/    /_ ) _       __  Informix Software Inc.  (303) 850-0210
_/__/ (_(_ (/ / (_(_  _/__> (-' -/~ (_-  5299 DTC Blvd #740; Englewood CO 80111
{uunet|pyramid}!infmx!dberg        The opinions expressed herein are mine alone.

gordon@osiris.cso.uiuc.edu (John Gordon) (01/10/91)

	Well, this may not be perfect since I don't have an AWK reference
right here, but I think this will work:

	(program that produces output) | awk '{print $2}'

	If that does not work, try this:

	awk -f <file that contains input> '{print $2}'

	Hope this helps.


---
John Gordon
Internet: gordon@osiris.cso.uiuc.edu        #include <disclaimer.h>
          gordon@cerl.cecer.army.mil       #include <clever_saying.h>

tchrist@convex.COM (Tom Christiansen) (01/10/91)

From the keyboard of dberg@informix.com (David I. Berg):
:In article <1991Jan9.162817.17038@porthos.cc.bellcore.com> snk@bae.bae.bellcore.com (Samuel N Kamens) writes:

:>The question I had was, how can I use UNIX tools to 
:>get one field out of a line?

:Use cut. 

% cut
cut: Command not found.
% awk
awk: Usage: awk [-fc] [-f source | 'cmds'] [files]

--tom

frechett@boulder.Colorado.EDU (-=Runaway Daemon=-) (01/10/91)

In article <5155@idunno.Princeton.EDU> pfalstad@phoenix.Princeton.EDU (Paul John Falstad) writes:
>In article <1991Jan9.225344.19979@informix.com> dberg@informix.com (David I. Berg) writes:
>>Use cut. The syntax is ` cut -fn -d" " ', where n is the number of the
>>field you wish to extract. -d" " specifies spaces as the delimiter between
>>fields. `man cut' will give you all the particulars.
>
>That isn't going to work for his example (output of lpq).  If you do:
>
>$ cut -d' ' -f3 <<EOF
>foo  bar   ble   boz  burn
>EOF
Ah, yes, you are right, but there is a way.  How about if you know that the
first column is only 12 characters wide... Something like 
cut -c1-12 <<EOF
in fact works quite well.  Granted, I would use awk or perl but
cut has its uses too.  Of course there are those without cut but that 
is their problem.

	ian

--
-=Runaway Daemon=-

tchrist@convex.COM (Tom Christiansen) (01/10/91)

From the keyboard of frechett@snoopy.Colorado.EDU (-=Runaway Daemon=-):
:Ah, yes, you are right, but there is a way.  How about if you know that the
:first column is only 12 characters wide... Something like 
:cut -c1-12 <<EOF
:in fact works quite well.  Granted, I would use awk or perl but
:cut has its uses too.  Of course there are those without cut but that 
:is their problem.

How it it our problem?  Cut isn't free, you know!

Honestly, I've never seen anyone do anything sufficently 
interesting about cut to justify paying for it, especially
since I've never seen any tricks that a straightforward 
awk or perl one-liner won't handle. Since I don't have it,
I don't really know, but this is my impression.

--tom

dnb@meshugge.media.mit.edu (David N. Blank) (01/11/91)

From: tchrist@convex.COM (Tom Christiansen):
> How it it our problem?  Cut isn't free, you know!

Actually, I found two free implementations of cut on uunet.uu.net:
   comp.sources.unix/volume8/cut+paste.Z (v08i078)
   bsd-sources/usr.bin/cut

               Peace,
                   dNb

painter@sequoia.execu.com (Tom Painter) (01/15/91)

In article <5155@idunno.Princeton.EDU> pfalstad@phoenix.Princeton.EDU (Paul John Falstad) writes:
>In article <1991Jan9.225344.19979@informix.com> dberg@informix.com (David I. Berg) writes:
>>Use cut. The syntax is ` cut -fn -d" " ', where n is the number of the
>>field you wish to extract. -d" " specifies spaces as the delimiter between
>>fields. `man cut' will give you all the particulars.
>
>That isn't going to work for his example (output of lpq).

Use the specific columns option to 'cut'

     cut -c8-15 filename

or
     lpq | cut -c8-15

which will cut out columns 8 through 15.

(Note:  'cut' and its companion 'paste' are Sys V tools.  Some BSD versions
        do have them.  You might look for an AT&T (att) bin on your system.)

Tom
-- 
-----------------------------------------------------------------------------
Tom Painter                             UUCP: ...!cs.utexas.edu!execu!painter
Execucom Systems Corp., Austin, Texas   Internet: painter@execu.com
(512) 327-7070                                    execu!painter@cs.utexas.edu
Disclaimer: My Company?  They'll claim all my waking hours, not my opinions.
-----------------------------------------------------------------------------

prc@erbe.se (Robert Claeson) (01/15/91)

In article <1991Jan10.012211.22100@ux1.cso.uiuc.edu>, gordon@osiris.cso.uiuc.edu (John Gordon) writes:

|> 	Well, this may not be perfect since I don't have an AWK reference
|> right here, but I think this will work:
|> 
|> 	(program that produces output) | awk '{print $2}'

|> 	If that does not work, try this:
|> 
|> 	awk -f <file that contains input> '{print $2}'

The former should work. The latter doesn't work. The -f flag to awk names a
file containing awk code to execute. But using "cut" is even easier (and
requires less typing) than using "awk".
-- 
Robert Claeson                  |Reasonable mailers: rclaeson@erbe.se
ERBE DATA AB                    |      Dumb mailers: rclaeson%erbe.se@sunet.se
Jakobsberg, Sweden              |  Perverse mailers: rclaeson%erbe.se@encore.com
Any opinions expressed herein definitely belongs to me and not to my employer.

prc@erbe.se (Robert Claeson) (01/15/91)

In article <1991Jan10.025736.17005@convex.com>, tchrist@convex.COM (Tom Christiansen) writes:
|> From the keyboard of dberg@informix.com (David I. Berg):

|> :Use cut. 

|> % cut
|> cut: Command not found.

Ah, but it is in real UNIX(tm), you know the one that comes from AT&T and that
says "System V" or something like that on the box (I've always wondered what
that "V" stands for -- "System Varies", perhaps?).

-- 
Robert Claeson                  |Reasonable mailers: rclaeson@erbe.se
ERBE DATA AB                    |      Dumb mailers: rclaeson%erbe.se@sunet.se
Jakobsberg, Sweden              |  Perverse mailers: rclaeson%erbe.se@encore.com
Any opinions expressed herein definitely belongs to me and not to my employer.