Linux awk Command: Analyze Text by Fields
awk divides input into records and fields and prints or calculates values that match conditions. It is useful for extracting columns, aggregating logs, and creating simple reports.
What is the awk command?
The default record is one line, and the default field separator is a sequence of whitespace characters. The entire current line is referred to as $0, the first field as $1, the number of fields as NF, and the cumulative line number as NR. Specify a delimiter with -F according to the input.
Basic syntax
awk [options] 'pattern { action }' [file...]
Installed implementations and options may vary depending on the distribution. Check the description of your current system with man awk.
Examples
Print the second field
Select only the desired field from a space-separated line.
awk '{ print $2 }' report.txt
Rows matching a condition
Print only rows where the third field is greater than 100.
awk '$3 > 100 { print $1, $3 }' report.txt
Sum Calculation
Add the second field of each row and output when the input ends.
awk '{ total += $2 } END { print total }' amounts.txt
Selecting rows based on field conditions
By default, awk interprets consecutive spaces as field separators. $2 is the second field, and you can print $1 for rows where the condition is true.
printf 'Ana 12\nBob 7\n' | awk '$2 >= 10 {print $1}'
Example output:
Ana
The value 7 in the second row does not satisfy the condition, so it is omitted. If columns are separated by specific characters such as tabs or colons, use -F to specify the delimiter. To handle CSV quoted fields with spaces or complex escapes, it is safer to use a dedicated CSV parser.
Aggregations accumulate state for each input row and print at the end. For example, awk '{sum += $2} END {print sum}' file totals the second field. Non-numeric text or missing fields can produce unexpected results, so check the input format before relying on the total.
Main Options and Format
| Options/Format | Description |
|---|---|
-F delimiter |
Specifies the input field separator. |
-v name=value |
Passes an initial value to an awk variable. |
BEGIN |
Pattern executed before reading the first input line. |
END |
Pattern executed after processing the last input line. |
Precautions when using
awk -F, cannot properly parse CSVs with commas inside quotes. If your input is CSV, use a dedicated parser. Features specific to GNU gawk may not work in other awk implementations.
Frequently Asked Questions
What is the difference between $0 and $1 in awk?
$0 is the entire current record, and $1 is the first field.
Official Documentation
You can check the exact behavior of options and differences between implementations in the official awk documentation.









