Command linehardAWK

Advanced AWK Programming

Powerful AWK scripts for data extraction, reporting, and complex text processing.

01

The problem

You need to process structured data, generate reports, or perform complex text transformations.

02

The solution

AWK
# Basic structure
awk 'pattern { action }' file.txt
awk 'BEGIN {initialization} pattern {action} END {finalization}' file.txt

# Built-in variables
awk '{print NR, NF, $0}' file.txt        # NR=record number, NF=fields
awk '{print FILENAME, FNR}' file.txt     # FNR=file record number
awk '{print $NF}' file.txt               # Last field
awk '{print $(NF-1)}' file.txt           # Second last field

# Field separator
awk -F: '{print $1}' /etc/passwd         # Colon separator
awk -F'[ :\t]' '{print $1}' file.txt    # Multiple separators
awk -v OFS='\t' '{print $1, $3}' file.txt # Output field separator

# Pattern matching
awk '/regex/ {print}' file.txt           # Lines matching regex
awk '!/exclude/ {print}' file.txt        # Lines NOT matching
awk '$1 ~ /^[0-9]+$/ {print}' file.txt   # Field matches regex
awk '$3 !~ /error/ {print}' file.txt     # Field doesn't match

# Comparisons
awk '$3 > 100 {print}' data.txt
awk '$2 == "ERROR" {print}' log.txt
awk '$4 <= 50 && $5 >= 10 {print}' data.txt
awk 'NR >= 10 && NR <= 20 {print}' file.txt

# Arithmetic operations
awk '{sum += $3} END {print sum}' data.txt
awk '{avg = total/NR} END {print avg}' data.txt
awk '{print $1, $2*1.1}' prices.txt      # Increase price by 10%

# String functions
awk '{print toupper($1), tolower($2)}' file.txt
awk '{print length($0), substr($1, 1, 3)}' file.txt
awk '{gsub(/old/, "new", $0); print}' file.txt
awk '{split($1, arr, ":"); print arr[1]}' file.txt

# Arrays (associative)
awk '{count[$1]++} END {for (item in count) print item, count[item]}' log.txt
awk '{sum[$1] += $2} END {for (key in sum) print key, sum[key]}' sales.txt

# Control structures
awk '{if ($3 > 100) print "High:", $0; else print "Normal:", $0}' data.txt
awk '{for (i=1; i<=NF; i++) if ($i == "target") print NR, $0}' file.txt
awk 'BEGIN {while (i++ < 10) print i}'

# Multi-file processing
awk '{print FILENAME, $0}' *.txt         # Process multiple files
awk 'FNR == 1 {print "---", FILENAME, "---"} {print}' *.txt

# Formatting output
awk 'BEGIN {printf "%-20s %10s\n", "Name", "Value"} {printf "%-20s %10.2f\n", $1, $2}' data.txt
awk '{print NR ". " $0}' file.txt        # Numbered lines

# Advanced examples
awk '!seen[$0]++' file.txt               # Remove duplicates
awk 'NR==FNR {a[$1]=$2; next} $1 in a {print $0, a[$1]}' file1 file2  # Join files
awk 'BEGIN {FS=OFS=","} {$3 = sprintf("%.2f", $3*1.08); print}' sales.csv  # Add tax

# Functions
awk 'function double(x) {return x*2} {print $1, double($2)}' data.txt

# Range patterns
awk '/start/,/end/ {print}' file.txt
awk 'NR==5,NR==10 {print}' file.txt

# Getline for advanced input
awk 'BEGIN {while ("date" | getline d) print d; close("date")}'
awk '{getline extra < "otherfile"; print $0, extra}' file.txt

# Redirection within awk
awk '{print $1 > "output.txt"; print $2 >> "append.txt"}' data.txt
awk '{print | "sort -nr"}' data.txt      # Pipe to shell command

03

Put it to work

Example
# Sum column, group by category
awk -F, '{sum[$1] += $2} END {for (cat in sum) print cat, sum[cat]}' sales.csv

# Apache log analysis
awk '{print $1}' access.log | sort | uniq -c | sort -nr | head -10

# CSV to formatted report
awk -F, 'BEGIN {print "Report\n======="} 
         NR>1 {total+=$3; print $1, "$" $3} 
         END {printf "\nTotal: $%.2f\n", total}' data.csv

# Extract data between markers
awk '/^START$/,/^END$/ {if (!/^START$/ && !/^END$/) print}' file.txt

# Calculate average by group
awk '{sum[$1]+=$2; count[$1]++} 
     END {for (group in sum) print group, sum[group]/count[group]}' grades.txt

# Merge two files on key
awk 'NR==FNR {map[$1]=$2; next} 
     $1 in map {print $0, map[$1]}' keys.txt data.txt

# Generate SQL insert statements
awk -F, 'BEGIN {print "INSERT INTO table VALUES"} 
         {printf "('''%s''', '''%s''', %d),\n", $1, $2, $3}' data.csv

# Process only every other line
awk 'NR % 2 == 0 {print}' file.txt

# Word frequency counter
awk '{for (i=1; i<=NF; i++) words[tolower($i)]++} 
     END {for (w in words) print w, words[w]}' text.txt | sort -k2nr

Worth knowing

AWK is a complete programming language. Use -v for variables. Next skips to next record. Getline for advanced input control. Arrays are associative. Use sprintf for formatting. For huge files, AWK is very efficient.