Awk Notes

Printing #

In Awk each line is a record and each column is a field.

You can print the whole line or individual fields like so.

i[akraker@localhost:~/awk]$ echo "one two three" | awk '{ print }'
one two three
i[akraker@localhost:~/awk]$ echo "one two three" | awk '{ print $1 }'
one
i[akraker@localhost:~/awk]$ echo "one two three" | awk '{ print $2 }'
two
i[akraker@localhost:~/awk]$ echo "one two three" | awk '{ print $3 }'
three

Note, fields aren’t 0 indexed and start at 1. The {print $0} statement returns the whole record:

i[akraker@localhost:~/awk]$ echo "one two three" | awk '{ print $0 }'
one two three

You can print specific fields from multiple records:

i[akraker@localhost:~/awk]$ echo "
i> one two three
i> four five six" \
i> | awk '{ print $1 }'

one
four
i[akraker@localhost:~/awk]$ echo "
one two three
four five six" | awk '{ print $1, $2 }'

one two
four five

Fields #

In Awk columns are called fields. Fields are indexed starting from 1.

In Awk there’s a special variable $NF which stands for Number of Fields.

That’s why this variable allows us to print the last field in a record because it represents the number of fields which is also the last column in the line.

i[akraker@localhost:~/awk]$ echo "
i> one two three
i> four five six" \
i> | awk '{ print $NF }'

three
six

Field Separators #

By default Awk assumes fields in a record are whitespace delimited. The field separator can be anything you want though.

Example:

Field separators can be defined by a regex too:

Records #

In Awk a record is a line, just like fields are columns. Records are also indexed starting from 1 just like with fields. Awk has the special variable $NR that stands for Number of Records. For each line in a file NR stands for the total number of records up to that point. It’s useful for printing line numbers:

i[akraker@localhost:~/awk]$ awk -F '\t' '{ print NR " " $(NF -2) }' bookreviews.tsv | head
1 review_headline
2 Five Stars
3 Please buy "I Saw a Friend"! Your children will be delighted!
4 Shipped fast.
5 Five Stars
6 Five Stars
7 PREDICTABLE ALMOST FROM PAGE 1
8 The Monastery Murders - Book 2: Devil in the Details
9 Five Stars
10 Five Stars

Pattern Matching #

Only print records that have a match:

i[akraker@localhost:~/awk]$ echo "aa
i> bb
i> cc" | awk '/bb/'
bb

Only print record with a match and specify field to print:

i[akraker@localhost:~/awk]$ echo "
i> aa 10
i> bb 20
i> cc 30" | awk '/bb/ { print $2 }'
20

Matching within fields #

Matching within a field:

awk '$4~/hello/ { print "This field contains hello", $4}'

Or field is an exact match:

awk '$4 == "hello" { print "This field is hello:", $4}'