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.

More in This Category
Bash Command Operators Explained: ;, &&, ||, &, and |

Bash Command Operators Explained: ;, &&, ||, &, and |

Understand how Bash operators run commands sequentially, conditionally, in the background, or through pipelines, with practical examples and precedence guidance.

Linux Tutorial / PID and PPID Explained: Find and Manage Processes

Linux Tutorial / PID and PPID Explained: Find and Manage Processes

Understand what PID and PPID mean, inspect parent-child process relationships with ps and pgrep, and safely terminate a process you started yourself.

Linux pwd Command: Print the Current Working Directory

Linux pwd Command: Print the Current Working Directory

Learn how to use the Linux pwd command to print the logical or physical absolute path of the current working directory, with essential options, practical examples, output interpretation, and common troubleshooting tips.

Linux ps Command: Inspect Running Processes

Linux ps Command: Inspect Running Processes

Learn how to inspect Running Processes with the Linux ps command, including practical examples, key options, and important precautions.

Linux whatis Command: Show One-Line Command Descriptions

Linux whatis Command: Show One-Line Command Descriptions

Learn how to use the Linux whatis command to display concise descriptions of commands and manual pages, with essential options, practical examples, output interpretation, and common troubleshooting tips.

Linux cut Command: Extract Characters and Fields

Linux cut Command: Extract Characters and Fields

Learn how to extract Characters and Fields with the Linux cut command, including practical examples, key options, and important precautions.

Linux type Command: Identify How a Command Is Resolved

Linux type Command: Identify How a Command Is Resolved

Learn how to use the Linux type command to identify aliases, functions, built-ins, keywords, and executable paths, with essential options, practical examples, output interpretation, and common troubleshooting tips.

Linux unzip Command: List and Extract ZIP Archives

Linux unzip Command: List and Extract ZIP Archives

Learn how to list and Extract ZIP Archives with the Linux unzip command, including practical examples, key options, and important precautions.

Linux apropos Command: Search Commands by Description

Linux apropos Command: Search Commands by Description

Learn how to use the Linux apropos command to find relevant manual pages by searching description keywords, with essential options, practical examples, output interpretation, and common troubleshooting tips.

Linux cmp Command: Compare Files Byte by Byte

Linux cmp Command: Compare Files Byte by Byte

Learn how Linux cmp compares two files byte by byte, reports the first mismatch, runs silently in scripts, limits ranges, and differs from diff and checksums.