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 fsUse 'fs' as the input field separator (default is whitespace).
-v var=valAssign value 'val' to variable 'var' before execution.
-f fileRead 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.txtCustom Delimiter (CSV)
Print the second column of a CSV file.
awk -F',' '{print $2}' users.csvFilter by Condition
Print lines where the third column is greater than 100.
awk '$3 > 100' sales.txtSum a Column
Calculate the sum of the numbers in the first column.
awk '{sum += $1} END {print sum}' numbers.txtPrint Line Numbers
Print the line number (NR) followed by the line content ($0).
awk '{print NR, $0}' file.txtLength Filter
Print lines longer than 80 characters.
awk 'length($0) > 80' file.txtBuilt for builders.