Intermediate · 12 min · practise every command in the built-in terminal

awk — Text Processing Language

awk is a complete programming language for processing column-based text data.

Basic syntax

awk 'pattern { action }' file

Print columns

awk '{print $1}' file.txt         # Print first column
awk '{print $1, $3}' file.txt     # Print columns 1 and 3
awk '{print $NF}' file.txt        # Print last column ($NF = number of fields)
awk '{print NR, $0}' file.txt     # Print line number and whole line

Custom delimiters

awk -F: '{print $1}' /etc/passwd  # Use : as separator
awk -F, '{print $2}' data.csv     # CSV processing

Conditions

awk '$3 > 100' file.txt           # Lines where col3 > 100
awk '/pattern/' file.txt          # Lines matching pattern
awk 'NR==5' file.txt              # Line 5 only
awk 'NR>=2 && NR<=5' file.txt     # Lines 2-5

Built-in variables

VariableMeaning
$0Entire line
$1..$nColumn 1..n
NFNumber of fields
NRCurrent line number
FSField separator

Practical examples

# Sum values in column 3
awk '{sum += $3} END {print sum}' file.txt

# Print username and UID from /etc/passwd
awk -F: '{print $1, $3}' /etc/passwd

# Average of column 2
awk '{sum+=$2; count++} END {print sum/count}' data.txt

Practice tasks

  1. Print only the usernames and user IDs from /etc/passwd
  2. Print the last column of /etc/passwd (login shell)
  3. Print the username and shell together using a custom field separator
  4. Use awk to filter /etc/passwd for lines matching a pattern

Commands in this lesson

Text Processing