Here’s a basic guide to awk with practical examples. awk is a powerful text-processing tool for parsing and manipulating structured data (like columns in files):
awk 'pattern { action }' [file]- Pattern: Optional condition (e.g.,
NR > 1for skipping headers). - Action: What to do when the pattern matches (e.g.,
print $1).
awk '{print}' file.txt # Print all linesawk '{print $1}' file.txt # Print first column
awk '{print $1, $3}' file.txt # Print first and third columns
awk '{print $NF}' file.txt # Print last column (NF = Number of Fields)awk '{print NR, $0}' file.txt # NR = current line number, $0 = entire lineawk '$3 > 50 {print}' file.txt # Print lines where column 3 > 50
awk '/error/ {print}' file.txt # Print lines containing "error"
awk '$1 ~ /^A/ {print}' file.txt # Print lines where column 1 starts with "A"awk '$2 == "FAIL" && $3 > 10 {print}' file.txtNR: Current line number.NF: Number of fields (columns) in the current line.FS: Field separator (default: whitespace).OFS: Output field separator (default: space).
awk '{print NF}' file.txt # Print number of columns per line
awk -F',' '{print $2}' file.csv # Use comma as field separator
awk 'BEGIN {FS=":"} {print $1}' /etc/passwd # Split by colonRun actions before or after processing lines:
# Add a header and footer
awk 'BEGIN {print "Start"} {print} END {print "End"}' file.txt
# Calculate total of column 1
awk '{sum += $1} END {print sum}' file.txtawk '{printf "Name: %-10s Score: %d\n", $1, $2}' file.txt
# %-10s = left-aligned string (10 characters), %d = integerawk '{print toupper($1)}' file.txt # Convert column 1 to uppercase
awk '{print substr($1, 1, 3)}' file.txt # Extract first 3 characters of column 1
awk '{print length($0)}' file.txt # Print line lengthawk '{if ($2 > 50) print "High"; else print "Low"}' file.txtFor complex tasks, save commands in a script (e.g., script.awk):
# script.awk
BEGIN {FS=","; OFS=" | "}
NR == 1 {print "Header:", $0; next}
{print NR, $1, $NF}Run it with:
awk -f script.awk data.csvawk -F',' '{print $1, $3}' data.csvawk '{sum += $2} END {print "Total:", sum}' sales.txtawk '/ERROR/ && $4 == "2023-10-01" {print}' app.logawk '{sum += $1; count++} END {print "Average:", sum/count}' numbers.txtName Age Score
Alice 25 90
Bob 30 85
Charlie 22 95
- Columns are 1-indexed (
$1,$2, etc.). $0refers to the entire line.- Use
-Fto specify a custom field separator (e.g.,-F':'for colon-separated files). awkworks on streams (you can pipe input to it).
With these basics, you can slice, filter, and transform text data efficiently!