How to process text with awk in Linux

· Category: Linux

Short answer

awk is a text-processing tool that operates on fields and records, ideal for extracting and transforming columnar data.

Steps

  1. Print a column:
awk '{print $1}' file.txt
  1. Specify a delimiter:
awk -F',' '{print $2}' data.csv
  1. Filter rows:
awk '$3 > 100 {print $0}' data.txt
  1. Sum a column:
awk '{sum += $1} END {print sum}' numbers.txt

Tips

  • awk processes line by line; BEGIN and END blocks run before and after.
  • Use -v to pass shell variables into awk.
  • awk is faster than Python for simple columnar transformations.

Common issues

  • Whitespace delimiter: awk treats multiple spaces as one by default.
  • $0 is the whole line; $1 is the first field.