awk examples

The Basics

An awk script usually takes the form

awk ‘pattern { action }’ file

where both pattern and action are optional. If there is no pattern, the action is performed on all lines. If there is action, the default action is to print all matching lines. Awk scripts are placed within single quotes when run in the command line to prevent the shell from interpreting it. Long awk scripts can be placed in a file and run as

awk -f the_script input_file

We can pass multiples to awk as well.

awk ‘pattern { action }’ file1 file2 file3

Examples:

Print all lines in a file(cat file)
awk ‘{print $0}’ file
awk ‘{print}’ file
awk ‘1’ file

Print the 5th column of every line
awk ‘{print $5}’ file

Print the 5th and 6th columns of every line
awk ‘{print $5,$6}’ file

Print the last column of every line
awk ‘{print $NF}’ file

Print the number of fields in a line
awk ‘{print NF}’ file

Print all lines in a file with line number
awk ‘{print NR”:”,$0}’ file

Print the number of lines in a file (wc -l)
awk ‘END {print NR}’ file

Print all lines in a file matching pattern
awk ‘/pattern/’ file

Print all lines in a file not matching pattern
awk ‘!/pattern/’ file

Print the 5th column of every line matching pattern.
awl ‘/2016/ {print $5}’ file

Print all lines beginning with the pattern.
awk ‘/^pattern/’ file

Print all lines matching multiple patterns
awk ‘/(pattern1|pattern2|pattern3)/’

Print all lines except empty lines.
awk ‘!/^$/’ file

Print other text in the output
awk ‘{print “this text”, $5, “as well as this”}’ file

Print formatting with printf.
Prints an 8 char wide field of strings and a number with two digits after decimal point.
awk ‘{printf “%10s %.2f \n”, $1, $2}’ file

Use multiple column separators
awk -F'[;,]’ ‘{print $X’ file

-F takes regular expressions as input.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

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

Google+ photo

You are commenting using your Google+ 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 )

w

Connecting to %s