AWK Command

AWK command named after its authors Aho, Weinberger, and Kernighan. This generates customized output based on scanning the rows and columns in a file.

It has 3 variants:

AWK – initial version from AT&T

NAWK – Improved version from AT&T

GAWK – Free Software Foundation version

Examples of AWK Command

Let’s take the example of a sample file abcd.txt

> cat abcd.txt

This is a: sample file: for showing: how: the cut: command is acting
Command: works. You: can select: any: range of: columns in the file
Columns from: the file, use: field: delimiters: etc to: extract more

  • To print all content of the file use awk ‘{print $0}’ abcd.txt

This is a: sample file: for showing: how: the cut: command is acting
Command: works. You: can select: any: range of: columns in the file
Columns from: the file, use: field: delimiters: etc to: extract more
The same result will be displayed with awk ‘{print}’ abcd.txt,

  • To print the first column of the file – awk ‘{print $1}’ abcd.txt

This
Columns
Command

  • To print specific columns of the file – awk ‘{print $1 $3$4}’ abcd.txt

 

This a: sample
Command You: can
Columns the file

  • To print all lines matching a specific pattern – awk ‘$4 == delimiters { print $0 }’ abcd.txt

Columns from: the file, use: field: delimiters: etc to: extract more

  • Pattern search using single keyword – awk ‘/select/’ abcd.txt

Command: works. You: can select: any: range of: columns in the file
awk ‘/etc/’ abcd.txt

Columns from: the file, use: field: delimiters: etc to: extract more

  • Pattern search using multiple keywords – awk ‘/select/

>/etc/’ abcd.txt [Here each search pattern has to be separated by a new line]
Columns from: the file, use: field: delimiters: etc to: extract more
awk ‘/showing/
/works/
/extract/’ abcd.txt
Command: works. You: can select: any: range of: columns in the file
Columns from: the file, use: field: delimiters: etc to: extract more
This is a: sample file: for showing: how: the cut: command is acting

  • Print if second column [ $2 ] matches string “works” – awk ‘$2 ~/works/’ abcd.txt

Command: works. You: can select: any: range of: columns in the file

  • Calling awk commands kept in one file to run via shell script – awk –f script.awk input_file
  • Print total number of rows in a file using NF – awk ‘END { print NR }’ abcd.txt

4

  • Print first 4 lines of file awk ‘NR < 5‘ abcd.txt

Cat abcd.txt
This is a: sample file: for showing: how: the cut: command is acting
This is a: sample file: for showing: how: the cut: command is acting
Command: works. You: can select: any: range of: columns in the file
Columns from: the file, use: field: delimiters: etc to: extract more
Command: works. You: can select: any: range of: columns in the file
Columns from: the file, use: field: delimiters: etc to: extract more

  • awk ‘NR < 5 ‘ abcd.txt

This is a: sample file: for showing: how: the cut: command is acting
This is a: sample file: for showing: how: the cut: command is acting
Command: works. You: can select: any: range of: columns in the file
Columns from: the file, use: field: delimiters: etc to: extract more

  • Print last line of a file – awk ‘END{print}’ abcd.txt

Columns from: the file, use: field: delimiters: etc to: extract more

  • Print line number 3 – awk ‘NR==3’ abcd.txt

Command: works. You: can select: any: range of: columns in the file

  • Print all lines having more than 10 char – awk ‘length > 10’ abcd.txt

This is a: sample file: for showing: how: the cut: command is acting
This is a: sample file: for showing: how: the cut: command is acting
Command: works. You: can select: any: range of: columns in the file
Columns from: the file, use: field: delimiters: etc to: extract more
Command: works. You: can select: any: range of: columns in the file
Columns from: the file, use: field: delimiters: etc to: extract more

  • Print all lines having less than 10 characters – awk ‘length < 10’ abcd.txt [no result]
  • Print a range of lines – awk ‘NR==2, NR==5’ abcd.txt . This will display 2nd to 5th lines.

This is a: sample file: for showing: how: the cut: command is acting
Command: works. You: can select: any: range of: columns in the file
Columns from: the file, use: field: delimiters: etc to: extract more
Command: works. You: can select: any: range of: columns in the file

  • Print total number of rows in a file – awk ‘END{print NR}’ abcd.txt

6

  • Print total number of words in a file – awk ‘{total = total + NF}; END {print total }’ abcd.txt

72

  • Print total number of lines that contains the string “columns” – awk ‘/columns/{n++}; END {print n +0}’ abcd.txt

2

  • Print last field of each line in a file – awk ‘{print $NF }’ abcd.txt

acting
acting
file
more
file
more

  • Print number of fields in each line along with the line – awk ‘{ print NF “:-” $0 } ‘ abcd.txt

13:-This is a: sample file: for showing: how: the cut: command is acting
13:-This is a: sample file: for showing: how: the cut: command is acting
12:-Command: works. You: can select: any: range of: columns in the file
12:-Command: works. You: can select: any: range of: columns in the file
11:-Columns from: the file, use: field: delimiters: etc to: extract more
11:-Columns from: the file, use: field: delimiters: etc to: extract more

  • Print last word of the last line – awk ‘{ field =$NF}; END { print field }’ abcd.txt

more

  • Print all rows deleting the 4th column of that row – awk ‘{ $4 = “”; print }’ abcd.txt

This is a: file: for showing: how: the cut: command is acting
This is a: file: for showing: how: the cut: command is acting
Command: works. You: select: any: range of: columns in the file
Columns from: the use: field: delimiters: etc to: extract more
Command: works. You: select: any: range of: columns in the file
Columns from: the use: field: delimiters: etc to: extract more

  • Remove duplicate, consecutive lines – awk ‘a !~ $0; {a=$0}’ abcd.txt

This is a: sample file: for showing: how: the cut: command is acting
Command: works. You: can select: any: range of: columns in the file
Columns from: the file, use: field: delimiters: etc to: extract more
Command: works. You: can select: any: range of: columns in the file
Columns from: the file, use: field: delimiters: etc to: extract more

  • Remove duplicate, non consecutive lines – awk ‘!a[$0]++’ abcd.txt

This is a: sample file: for showing: how: the cut: command is acting
Command: works. You: can select: any: range of: columns in the file
Columns from: the file, use: field: delimiters: etc to: extract more

  • Delete all blank lines from the file – awk NF
  • cat abcd.txt
    This is a: sample file: for showing: how: the cut: command is acting
    This is a: sample file: for showing: how: the cut: command is acting
    Command: works. You: can select: any: range of: columns in the file
    Columns from: the file, use: field: delimiters: etc to: extract more
    Command: works. You: can select: any: range of: columns in the file
    Columns from: the file, use: field: delimiters: etc to: extract more
    ßblank rowsßblank rowsawk NF abcd.txt
    This is a: sample file: for showing: how: the cut: command is acting
    This is a: sample file: for showing: how: the cut: command is acting
    Command: works. You: can select: any: range of: columns in the file
    Columns from: the file, use: field: delimiters: etc to: extract more
    Command: works. You: can select: any: range of: columns in the file
    Columns from: the file, use: field: delimiters: etc to: extract more

    AWK comes up with some built –in variables

    FS – The Input Field Separator Variable
    OFS – The Output Field Separator Variable
    NF – The Number of Fields Variable
    NR – The Number of Records Variable
    RS – The Record Separator Variable
    ORS – The Output Record Separator Variable
    FILENAME – The current FILENAME Variable