awk Commands Cheat Sheet

• Updated January 10, 2019

awk - which is not an acronym for anything other than the first letter in the three creator’s last names - is a text processor and is included with practically every Unix-like system. It has its own programming language that can be used to parse through text line by line. It has no problem parsing through a large amount of text data that other programs might choke on.

If you have never used awk, I recommend reading through Greg Grothaus’ “Why you should know just a little awk”.

This post will be an ever growing list of awk commands I use most frequently.

Print the first column from all rows:

awk '{print $1}'

Print the first and third columns from all rows:

awk '{print $1, $3}'

The comma between the column parameters in the previous command will put a space between the outputted columns. However, you can change this behavior and use your own formatting:

awk '{print $1 " --- " $3}'

Change the field delimiter and print the second column

By default, awk will parse a row into columns using a space as the delimiter. The delimiter can be changed with the -F command line switch.

For example, change the delimiter to a semicolon:

awk -F: '{print $2}'

The field delimiter could be anything such as an equal sign, -F=, or a period, -F..

There are plenty of situations where you might need to print the last column from a given row, but you do not know how many columns are on that given row. The built-in variable NF can be used to solve this.

awk '{print $NF}'

Another built-in variable is NR which always contains the current row number. This can be used to do such things as printing the last column from only the first row:

awk 'NR==1 {print $NF}'

If you want to print particular columns only from rows that match certain conditions, you can pass a regular expression:

awk '/regular-expression-to-match/ {print $1}'

You can also invert your regular expression match by putting an exclamation mark outside of the search field:

awk '!/regular-expression-to-match/ {print $1}'

If you found this post useful and would like to help support this site - and get something for yourself - sign up for any of the services listed below through the provided affiliate links. I will receive a referral payment from any of the services you sign-up for.

Get faster shipping and more with Amazon Prime: About to order something from Amazon but want to get more value out of the money you would normally pay for shipping? Sign-up for a free 30-day trial of Amazon Prime to get free two-day shipping, access to thousands of movies and TV shows, and more.

Thanks for reading and take care.