The awk command in Linux reads text line by line, splits each line into fields and runs actions on lines that match a pattern. For example, awk '{print $1, $3}' data.txt prints the first and third column of every line. It is the standard tool for processing columnar data such as logs, CSV-like files and command output.
An awk program is a list of pattern { action } pairs. For each input line awk tests every pattern and, when it matches, runs the action. This guide uses GNU awk (gawk) or the compatible awk found on most distributions.
awk calls each line a record and each whitespace-separated piece a field. Inside an action, $0 is the whole line, $1 is the first field, $2 the second, and so on. NF is the number of fields on the line and NR is the current line number.
Create a sample file.
Print the last field with $NF and the number of fields with NF.
A pattern in front of the action selects the lines to run it on. It can be a regular expression, a comparison or a range.
If you write only a pattern, awk prints the matching lines. If you write only an action, it runs on every line. Two special patterns, BEGIN and END, run once before the first line and after the last line.
awk variables need no declaration, start at zero and handle numbers automatically. That makes summing a column a one-liner.
For a count and average, track both values and print them in END.
NR > 1 skips the header line. Always guard the division in END when the file might be empty, because dividing by zero is a fatal error in awk.
awk arrays are associative: the index can be any string. This gives you the equivalent of GROUP BY in SQL.
The order of a for ... in loop is not guaranteed. Pipe the output to sort when order matters.
The default separator is any run of spaces or tabs. Use -F to set another one, for instance a comma, colon or tab.
FS is the input field separator and OFS the output separator used by print when you separate arguments with commas. Plain -F, does not understand quoted CSV fields that contain commas. For real CSV, use a CSV-aware tool such as csvkit or GNU awk's --csv option available in gawk 5.3 and later.
print is quick, while printf gives exact control over widths and decimals, like C.
Remember that printf does not add a newline for you, so end the format with \n.
awk ships with string and math functions that cover most text jobs.
| Function | Purpose |
|---|---|
length(s) | Length of a string or of $0 |
substr(s, i, n) | Substring starting at position i |
index(s, t) | Position of t in s |
split(s, a, sep) | Split into array a |
toupper(s), tolower(s) | Change case |
sub(re, repl), gsub(re, repl) | Replace first or all matches |
When a program grows, store it in a file and run it with awk -f report.awk sales.txt. Pass shell values in safely with -v.
sed edits text with patterns and substitutions. awk understands fields, variables and arithmetic, so it is better when you need columns, totals or conditions on values.
Use $NF, as in awk '{print $NF}' file. To print the second to last column, use $(NF-1).
Single quotes stop the shell from expanding variables. Pass the value with -v name="$value" and use name inside the awk program.
Add the condition NR > 1, or use NR == 1 {next} as the first rule.
awk splits each line into fields, runs pattern { action } rules and keeps variables and associative arrays, which makes it ideal for columnar data. Use $1, $NF and NR to address data, -F to change the separator, END blocks for totals, arrays for grouping and printf for tidy output. Reach for awk whenever a task is about columns rather than just text substitution.