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:
    - 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:
    - 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:
    - 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:
    - section: data
      field: pattern
      type: string
      example: Print columns
    - section: data
      field: command
      type: string
      example: awk '{print $1, $3}'
    - section: data
      field: description
      type: string
      example: Print the 1st and 3rd fields of each line.
data:
  - pattern: Print columns
    command: awk '{print $1, $3}'
    description: Print the 1st and 3rd fields of each line.
  - pattern: Field separator
    command: "awk -F: '{print $1}' /etc/passwd"
    description: Use colon as delimiter; print 1st field.
  - pattern: Built-in variables
    command: NR, NF, FS, OFS, RS, ORS, FILENAME
    description: NR=record number, NF=field count, FS=OFS=field sep, RS=record sep.
  - pattern: Conditional print
    command: awk '$3 > 100 {print $1}'
    description: Print $1 only when $3 exceeds 100.
  - pattern: Sum a column
    command: awk '{sum += $2} END {print sum}'
    description: Accumulate sum of column 2 and print at END.
  - pattern: Pattern matching
    command: awk '/error/ {print}' log.txt
    description: Print lines matching the regex /error/.
  - pattern: Functions
    command: length(), sub(), gsub(), split(), systime()
    description: Built-in string and time functions.
  - pattern: Multi-character FS
    command: "awk -v FS=\"\\t\" '{print $2}'"
    description: Set field separator to tab using -v flag.
  - pattern: Print last field
    command: awk '{print $NF}' file
    description: NF is the number of fields, so $NF is the last one.
  - pattern: Remove duplicate lines
    command: awk '!seen[$0]++' file
    description: Prints each line only the first time it appears, preserving order.
  - pattern: Print a line range
    command: awk 'NR==2,NR==5' file
    description: Prints lines 2 through 5 using the record-number variable NR.
  - pattern: Average a column
    command: awk '{s+=$2} END {print s/NR}' file
    description: Accumulates column 2 and divides by the number of records at the end.
  - pattern: Maximum of a column
    command: awk 'NR==1 || $3>m {m=$3} END {print m}' file
    description: Tracks the largest value seen in column 3.
  - pattern: Substitute text
    command: "awk '{gsub(/foo/,\"bar\"); print}' file"
    description: Replaces all occurrences of foo with bar in each line.
  - pattern: Count matching lines
    command: awk '/error/ {n++} END {print n+0}' file
    description: Counts lines containing 'error'; n+0 prints 0 when none match.
  - pattern: Filter by field length
    command: awk 'length($1) > 5' file
    description: Prints lines whose first field is longer than five characters.
