Filter lines by pattern

Print the line number and text of lines with ERROR

awk '/ERROR/ {print NR": "$0}' app.log

tool
awk15 commands
tags
awklinuxdebug

More awk commands

  • awk '{sum += $4} END {print sum}' access.log

    Add up a numeric column (here the 4th) and print the total

  • awk '/START/,/END/' file.txt

    Print lines from START to END, both included

  • awk 'NR>1 {print $1, $2}' data.csv

    Process all lines except the first one (header)

  • awk '{count[$1]++} END {for (k in count) print count[k], k}' access.log | sort -rn

    Count how often each first field appears, for example IP addresses

  • awk '{print $NF}' file.txt

    NF is the number of fields, so $NF is the last field

all 15 awk commands