Using Awk for Text Processing in Linux

Awk turns plain text into structured results with minimal typing.

Awk Mental Model

Awk reads input line by line, splits it into fields, then runs actions on patterns. Think: pattern { action }.

echo "Ada Lovelace" | awk '{{print $2, $1}}'

Fields and Separators

Default field separator is whitespace. Change it with -F or by setting FS.

awk -F, '{{print $1 " -> " $3}}' data.csv

Built in Variables

awk '{{print NR, NF, $0}}' file.txt

Conditions and Filtering

awk '$3 > 90 {{print $1, $2}}' scores.txt

Math and Aggregations

awk -F, '{{sum+=$3}} END {{print "Total:", sum}}' sales.csv

Format Output

awk -F: '{{printf "%-20s %s\n", $1, $7}}' /etc/passwd

Small Scripts

Awk one liners grow up fast. Put logic in a file for reuse.

cat normalize.awk
/^\s*#/ {{next}}          # skip comments
{{gsub(/\s+/, " ")}}      # collapse spaces
{{print tolower($0)}}
awk -f normalize.awk input.txt

Awk vs Grep vs Sed

Use grep to find, sed to replace, awk to compute. They are teammates, not rivals.

The VPN Index - Find the best VPN for your needs