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