Mailing Lists: Apple Mailing Lists
Image of Mac OS face in stamp
Re: Shell grep help - how to get only first occurrence of match?
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Shell grep help - how to get only first occurrence of match?




On 29/03/2007, at 3:57 AM, Mark J. Reed wrote:

If you have columns of data separated by arbitrary amounts of
whitespace, the command you're looking for is awk, rather than grep.

awk '{print $1}' will print the first column of every line.  $2 for
the second, etc.
$NF (short for "Number of Fields") for the last column, $(NF-1) for
the second-to-last, etc.
$0 is the whole line.

you can stick /pattern/ in front of the {...} block to run it only for
lines matching that pattern, e.g.

awk '/[0-9]/ {print $1}'  will print the first column of all lines
that contain a digit anywhere in the line.  Note that this means it is
never necessary to do grep | awk. :)


Wow, it's that easy. You wouldn't know it from the man page.

malcolm

_______________________________________________
Do not post admin requests to the list. They will be ignored.
AppleScript-Users mailing list      (email@hidden)
Help/Unsubscribe/Update your Subscription:
Archives: http://lists.apple.com/archives/applescript-users

This email sent to email@hidden
References: 
 >Re: Shell script help - get date of last modified file in hierarchy (From: "Mark J. Reed" <email@hidden>)
 >Shell grep help - how to get only first occurrence of match? (From: Richard Rönnbäck <email@hidden>)
 >Re: Shell grep help - how to get only first occurrence of match? (From: "Mark J. Reed" <email@hidden>)



Visit the Apple Store online or at retail locations.
1-800-MY-APPLE

Contact Apple | Terms of Use | Privacy Policy

Copyright © 2011 Apple Inc. All rights reserved.