Count occurrences per key

Frequency count of the first field (e.g. IP addresses)

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

tool
awk15 commands
tags
awklinuxnetwork

More awk commands

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

    NF holds the number of fields; $NF is the last field

  • awk '$2 > 500 && $5 == "POST"' access.log

    Print lines where field 2 > 500 AND field 5 is POST

  • awk '{printf "%-20s %5d\n", $1, $2}' data.txt

    Aligned printf formatting with fixed column widths

  • awk -f transform.awk input.txt

    Load awk program from an external script file

  • awk '{gsub(/foo/, "bar"); print}' input.txt > tmp && mv tmp input.txt

    Global substitution and write back to same file

all 15 awk commands