awk

A domain-specific language designed for text processing and data extraction. It treats files as columns (fields) and rows (records).

Synopsis

awk [options] 'script' file(s)

Core Options

-F fs

Use 'fs' as the input field separator (default is whitespace).

-v var=val

Assign value 'val' to variable 'var' before execution.

-f file

Read the AWK script from the specified file.

Usage Examples

Print Specific Columns

Print the first and third columns of a file (space-separated).

awk '{print $1, $3}' data.txt

Custom Delimiter (CSV)

Print the second column of a CSV file.

awk -F',' '{print $2}' users.csv

Filter by Condition

Print lines where the third column is greater than 100.

awk '$3 > 100' sales.txt

Sum a Column

Calculate the sum of the numbers in the first column.

awk '{sum += $1} END {print sum}' numbers.txt

Print Line Numbers

Print the line number (NR) followed by the line content ($0).

awk '{print NR, $0}' file.txt

Length Filter

Print lines longer than 80 characters.

awk 'length($0) > 80' file.txt
Built for builders.