text
awk
Pattern-action language for processing columnar text. Excellent for reshaping tabular data, aggregating fields, and quick reports.
awk [OPTION]... 'program' [FILE]... Common flags
| Flag | Purpose |
|---|---|
| -F | Set the input field separator |
| -v | Assign an awk variable from the shell (e.g. -v key=value) |
| -f | Read the awk program from a file |
| -e | Append a program-text fragment (GNU awk) |
Examples
awk '{print $1, $NF}' access.log Print the first and last whitespace-separated fields per line
awk -F: '$3 >= 1000 {print $1}' /etc/passwd List non-system usernames using : as the separator
awk '{sum += $2} END {print sum}' sales.tsv Sum the second column
ps -eo rss,cmd | awk '{m[$2]+=$1} END{for(c in m) print m[c], c}' Aggregate RSS by command
Gotcha
Fields default to whitespace-splitting; if your data has empty fields, use -F with an explicit delimiter and check FS/OFS semantics.
Related commands
sed
Stream editor that applies a small program to each line of input. Widely used for search-and-replace and inline file edits.
cut
Extracts sections from each input line by byte, character, or delimited field. Handy for pulling columns out of CSV or colon-delimited files.
grep
Searches text for lines matching a regular expression. Central to log analysis and pipeline filtering.
cat
Concatenates files and prints them to standard output. Also useful as a simple viewer or to feed content into pipelines.
sort
Sorts lines of text. Supports numeric, human-numeric, reverse, unique, and keyed sorting.