Get Started With Awk in Bash

Awk can be very useful when working with text files in Bash. This is a short cheat sheet on some of the commands you’ll most often use.

The Basics of awk

To use awk, you often work with textfiles. The text file could look something like this.

ajay manager account 45000
sunil clerk account 25000
varun manager sales 50000
amit manager account 47000
tarun peon sales 15000
deepak clerk sales 23000
sunil peon sales 13000
satvik director purchase 80000

What awk basically does, is handle each line, one by one. It also separates the fields in a line so you can get your specified value. By default, you’ll use whitespace as a separator.

The syntax should look something like this

# with a file
awk '{print}' test.txt

# with pipes
echo "some text" | awk '{print}'

It is important to use ' instead of " to make it work.

Printing fields

You can use awk in many different ways. The most common is printing one or more separate fields. Each field is given a variable you can use. This applies to all lines, not only the first one used in this example.

 $1    $2      $3     $4
ajay manager account 45000

If you want to print the whole line, you’ll use $0. Here are some examples of how you can use it.

# print whole line
awk '{print $0}' test.txt

# print only first field 
awk '{print $1}' test.txt

# print first and last
awk '{print $1, $4}' test.txt

You can also filter to write fields that only contain a specific text.

# print only lines containing the word "manager"
awk '/manager/ {print}' test.txt

# print only lines containing the word "manager" or "clerk2
awk '/manager|clerk/ {print}' test.txt

Built-in variables

There are some variables you can use as well.

Some basic usages with the variables

# display last field only
awk '{print $NF}' test.txt

# display only first line
awk 'NR==1 {print $0}' test.txt

# display line from 3 to 6
awk 'NR==3, NR==6 {print $0}' test.txt

# display each line with line number before
awk '{print $NR,$0}' test.txt

# print all lines with more than 0 fields
awk 'NF > 0' test.txt

# print lines with more than 0 characters
awk 'length($0) > 10' test.txt

# print line if first field is "ajay"
awk '$1 == "ajay" {print $0}' test.txt

Change field separator

By dafult, each field is separated by a whitespace. If you want to use something else, you can specify it with the F flag.

# use = as separator
awk -F '=' '{print $1}'

# use + or = as separator
awk -F '[+=]' '{print $1}'