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
Linux fsck Command: Check and Repair Filesystems

Linux fsck Command: Check and Repair Filesystems

Learn how to check and Repair Filesystems with the Linux fsck command, including practical examples, key options, and important precautions.

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 / Manage Files and Directories: Create, Copy, Move, and Delete

Linux Tutorial / Manage Files and Directories: Create, Copy, Move, and Delete

Practice creating, copying, renaming, moving, and deleting files and directories in a dedicated folder while checking each result safely.

Linux bg Command: Resume a Shell Job in the Background

Linux bg Command: Resume a Shell Job in the Background

Learn how to resume a Shell Job in the Background with the Linux bg command, including practical examples, key options, and important precautions.

Linux paste Command: Join Corresponding Lines Side by Side

Linux paste Command: Join Corresponding Lines Side by Side

Learn how to join Corresponding Lines Side by Side with the Linux paste command, including practical examples, key options, and important precautions.

How to Check the Linux Distribution, Version, Kernel, and Architecture

How to Check the Linux Distribution, Version, Kernel, and Architecture

Learn how to identify a Linux distribution and version with /etc/os-release, then check the running kernel and CPU architecture with hostnamectl and uname. The guide also covers scripts, legacy systems, containers, WSL, and chroot environments.

Linux apk Command: Manage Packages on Alpine Linux

Linux apk Command: Manage Packages on Alpine Linux

Learn how to manage Packages on Alpine Linux with the Linux apk command, including practical examples, key options, and important precautions.

Linux time Command: Measure Command Execution Time

Linux time Command: Measure Command Execution Time

Learn how to use the Linux time command to measure elapsed, user CPU, and system CPU time for a command, with essential options, practical examples, output interpretation, and common troubleshooting tips.

Linux umount Command: Unmount a Filesystem Safely

Linux umount Command: Unmount a Filesystem Safely

Learn how to unmount a Filesystem Safely with the Linux umount command, including practical examples, key options, and important precautions.

Linux chmod Command: Change File and Directory Permissions

Linux chmod Command: Change File and Directory Permissions

Learn how to change File and Directory Permissions with the Linux chmod command, including practical examples, key options, and important precautions.