api: YAML JSON TOON Database version: 1.0.0 format: json dataset: slug: awk-reference title: AWK Reference description: "Essential AWK commands, built-in variables, and common patterns." summary: "AWK one-liners and patterns for working with columns and records: printing fields, setting the field separator, built-in variables, conditionals, sums, pattern matching and functions, extended with last-field printing, de-duplication, line ranges, averages, maxima and substitutions." use_when[2]: You need a one-line column or record transformation on text or CSV-like data.,You need the idiom for a common AWK task. caveats[2]: "AWK implementations differ (GNU awk, mawk, BSD awk); gawk-specific features are not portable.","For quoted CSV with embedded commas, use a proper CSV tool; awk -F, splits naively." related[4]: sed-reference,unix-text-processing-commands,file-operations,string-algorithms category: Unix Commands category_slug: unix-commands tags: "unix,awk,text-processing,scripting" view_count: 0 entry_count: 16 fields[3]{section,field,type,example}: data,pattern,string,Print columns data,command,string,"awk '{print $1, $3}'" data,description,string,Print the 1st and 3rd fields of each line. data[16]{pattern,command,description}: Print columns,"awk '{print $1, $3}'",Print the 1st and 3rd fields of each line. Field separator,"awk -F: '{print $1}' /etc/passwd",Use colon as delimiter; print 1st field. Built-in variables,"NR, NF, FS, OFS, RS, ORS, FILENAME","NR=record number, NF=field count, FS=OFS=field sep, RS=record sep." Conditional print,"awk '$3 > 100 {print $1}'",Print $1 only when $3 exceeds 100. Sum a column,"awk '{sum += $2} END {print sum}'",Accumulate sum of column 2 and print at END. Pattern matching,"awk '/error/ {print}' log.txt",Print lines matching the regex /error/. Functions,"length(), sub(), gsub(), split(), systime()",Built-in string and time functions. Multi-character FS,"awk -v FS=\"\\t\" '{print $2}'",Set field separator to tab using -v flag. Print last field,"awk '{print $NF}' file","NF is the number of fields, so $NF is the last one." Remove duplicate lines,"awk '!seen[$0]++' file","Prints each line only the first time it appears, preserving order." Print a line range,"awk 'NR==2,NR==5' file",Prints lines 2 through 5 using the record-number variable NR. Average a column,"awk '{s+=$2} END {print s/NR}' file",Accumulates column 2 and divides by the number of records at the end. Maximum of a column,"awk 'NR==1 || $3>m {m=$3} END {print m}' file",Tracks the largest value seen in column 3. Substitute text,"awk '{gsub(/foo/,\"bar\"); print}' file",Replaces all occurrences of foo with bar in each line. Count matching lines,"awk '/error/ {n++} END {print n+0}' file",Counts lines containing 'error'; n+0 prints 0 when none match. Filter by field length,awk 'length($1) > 5' file,Prints lines whose first field is longer than five characters.