You are on page 1of 3

Awk

Examples:

1) Display lines from my_file containing the string "123" or "abc" or "some text":

awk '/123/ { print $0 }

/abc/ { print $0 }

/some text/ { print $0 }' my_file

A regular expression enclosed in slashes (`/') is an `awk' pattern that matches every input record whose
text belongs to that set. E.g. the pattern /foo/ matches any input record containing the three characters
`foo', *anywhere* in the record.

Awk Patterns: `awk' patterns may be one of the following:

/Regular Expression/ - Match =

Pattern && Pattern - AND

Pattern || Pattern - OR

! Pattern - NOT

Pattern ? Pattern : Pattern - If, Then, Else

Pattern1, Pattern2 - Range Start - end

BEGIN - Perform action BEFORE input file is read

END - Perform action AFTER input file is read

The special patterns BEGIN and END may be used to capture control before the first input line is read
and after the last. BEGIN and END do not combine with other patterns.

Variable names with special meanings:

CONVFMT conversion format used when converting numbers (default %.6g)

FS regular expression used to separate fields; also settable by option -Ffs.

NF number of fields in the current record

NR ordinal number of the current record

FNR ordinal number of the current record in the current file

FILENAME the name of the current input file


RS input record separator (default newline)

OFS output field separator (default blank)

ORS output record separator (default newline)

OFMT output format for numbers (default %.6g)

SUBSEP separates multiple subscripts (default 034)

ARGC argument count, assignable

ARGV argument array, assignable; non-null members are taken as filenames

ENVIRON array of environment variables; subscripts are names.

In addition to simple pattern matching `awk' has a huge range of text and arithmetic Functions,
Variables and Operators.

`gawk' will ignore newlines after any of the following: , { ? : || && do else

Comments -> start with a `#', and continue to the end of the line:

# This program prints a nice friendly message

Examples

1) Print the Row Number (NR), then a dash and space ("- ") and then the first item ($1) from each
line in samplefile.txt:

$ awk '{print NR "- " $1 }' samplefile.txt

2) Print the first item ($1) and then the third last item $(NF-2) from each line in samplefile.txt:

$ awk '{print $1, $(NF-2) }' samplefile.txt

3) Print every line that has at least one field. This is an easy way to delete blank lines from a file (or
rather, to create a new file similar to the old file but from which the blank lines have been
deleted)

awk 'NF > 0' data.txt

Comparison with grep:

Running grep Dec against the following file listing would return the 3 rows shown in bold as it matches
text in different places:
-rw-r--r-- 7 simon simon 12043 Jan 31 09:36 December.pdf
-rw-r--r-- 3 simon simon 1024 Dec 01 11:59 README
-rw-r--r-- 3 simon simon 5096 Nov 14 18:22 Decision.txt

4) Running -> awk '$6 == "Dec" ' against the same file listing, the relational operator $6 matches the
exact field (column 6 = Month) so it will list only the December file:

$ ls -l /tmp/demo | awk '$6 == "Dec"'

5) Print the length of the longest input line:

awk '{ if (length($0) > max) max = length($0) }

END { print max }' data

6) Print seven random numbers from zero to 100, inclusive:

awk 'BEGIN { for (i = 1; i <= 7; i++)

print int(101 * rand()) }'

7) Print the total number of bytes used by FILES:

ls -lg FILES | awk '{ x += $5 } ; END { print "total bytes: " x }'

8) Print the average file size of all .PNG files within a directory:

ls –l *.png | gawk '{sum += $5; n++;} END {print sum/n;}'

9) Print a sorted list of the login names of all users:

awk -F : '{ print $1 }' /etc/passwd | sort

10) Count the lines in a file:

awk 'END { print NR }' data.c

11) Print the even numbered lines in the data file. If you were to use the expression 'NR % 2 == 1'
instead, it would print the odd numbered lines.

awk 'NR % 2 == 0' data

Reference: http://ss64.com/bash/awk.html

You might also like