File Viewing & Editing

awk

Pattern scanning and processing language for structured text data.

Synopsis

syntax
awk [OPTION]... 'PROGRAM' [FILE]...

Examples

Print first field of each line
awk '{print $1}' file.txt
Print columns 2 and 4 from CSV
awk -F',' '{print $2, $4}' data.csv
Count lines containing 'error'
awk '/error/ {count++} END {print count}' log.txt
Sum first column values
awk '{sum+=$1} END {print sum}' numbers.txt
Print lines 10 through 20
awk 'NR==10,NR==20' file.txt

Common options

FlagDescription
-FSet field separator
-vAssign variable before execution
-fRead program from file
-iInclude awk source library

About awk

The `awk` command pattern scanning and processing language for structured text data. Text viewing and editing commands are fundamental tools in any Linux user's toolkit.

Linux treats almost everything as a file, so the ability to quickly inspect, filter, transform, and edit file contents from the command line is critical. These commands are regularly combined with pipes and redirects to build powerful data-processing pipelines.

The command accepts 4 commonly used flags shown above, though the full set of options is available in the man page (`man awk`). The 5 examples on this page cover typical real-world usage patterns that you can copy and adapt for your own workflows.

Related commands

More File Viewing & Editing Commands

Other commands in the File Viewing & Editing category

Related tools