Interview Prep/Shell Scripting/Text processing: grep, sed, awk, cut, sort, uniq

What is the structure of an `awk` program?

BeginnerConceptText processing: grep, sed, awk, cut, sort, uniq
Answer

awk 'pattern { action }' file: for each input line (record), split into fields $1..$NF (by whitespace or -F), and run the action when the pattern is true. BEGIN runs before input, END after.

awk -F: '$3 >= 1000 && $7 !~ /nologin/ {print $1, $6}' /etc/passwd
awk '{bytes += $10} END {printf "%.1f MB\n", bytes / 1024 / 1024}' access.log
awk 'NR > 1 && $5+0 > 80 {print $6, $5}' <(df -P)

Built-ins worth knowing: NR (record number), NF (field count), $NF (last field), FS/OFS, FILENAME.

All Shell Scripting questions Roadmap: Shell scripting (Bash)

More on Text processing: grep, sed, awk, cut, sort, uniq