awk

I can never remember the syntax for the feature of awk which I use most frequently: that of matching a regular expression and printing part of the matching line. For example, suppose you had a data file containing lines with 3 possible outcomes of a program: good, bad, and ugly. Also suppose you want to print out only the second number for each of the “good” lines.


good 1 2
bad 3 4
ugly 5 6
good 32 54
bad 46 78
ugly 90 89
...
good 10 20
bad 30 40
ugly 50 60

The command:

cat file.ext | awk '/good/ {print $3}'

will print out:

2
54
20

Advertisements

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s