Path: utzoo!utgpu!news-server.csri.toronto.edu!cs.utexas.edu!convex!news From: tchrist@convex.COM (Tom Christiansen) Newsgroups: comp.unix.questions Subject: Re: getting a field from a line in awk/sed/? Message-ID: <1991Jan09.174326.23636@convex.com> Date: 9 Jan 91 17:43:26 GMT References: <1991Jan9.162817.17038@porthos.cc.bellcore.com> Sender: news@convex.com (news access account) Reply-To: tchrist@convex.COM (Tom Christiansen) Organization: CONVEX Software Development, Richardson, TX Lines: 44 Nntp-Posting-Host: pixel.convex.com 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