Most log questions (top N, count per X, filter by Y) are answered by chaining small Unix filters into one pipeline. This page shows the standard pipeline shape, then the two filters that need the most explanation: awk for picking fields and uniq for collapsing duplicates.

Text-processing pipelines

A handful of small Unix filters, chained into one pipeline over a log file, answer most “top N”, “count per X”, and “filter by Y” questions.1 Each stage does one thing: select a field, group identical values, count them, rank them.

The standard shape

awk '{print $1}' Bash/access.log | sort | uniq -c | sort -nr | head -5 finds the top five client IPs: awk prints field 1, sort groups identical values, uniq -c counts adjacent duplicates, sort -nr ranks numerically in reverse, and head -5 keeps five lines.

QuestionPipeline
Requests per pathcut -d'"' -f2 then cut -d' ' -f2, then sort | uniq -c | sort -nr
Distinct IPsawk '{print $1}' | sort -u | wc -l
Exact path matchgrep ' /api/ads HTTP/', so /api/ads-v2 does not match
Requests per methodcut -d'"' -f2 | cut -d' ' -f1 | sort | uniq -c | sort -nr
Top memory processesps aux --sort=-%mem | head -6 on Linux

As given in the quick reference.1

Pitfalls

  • uniq only collapses adjacent identical lines, so it normally needs sorted input.2
  • Quotes do not stop awk from splitting on spaces, and User-Agent values contain spaces, so later field numbers are unstable; cut on the quote character instead.3
  • GNU and BSD options differ: ps --sort is Linux-only, and macOS needs ps aux | tail -n +2 | sort -k4 -nr.1

awk

awk processes text one line at a time: it splits each line into fields and runs condition { action } rules against them. The name comes from its authors, Aho, Weinberger, and Kernighan.3

Essentials

SyntaxMeaning
$1, $2First and second fields (whitespace-separated by default)
$0The whole line
NF, $NFNumber of fields; the last field
NRCurrent line number
-F:Use another delimiter, such as : for /etc/passwd

As described in the note. Examples: awk '$9 >= 500 {print $1, $9}' prints the IP and status of 5xx requests in the sample log.3

Finding a field number

awk 'NR == 1 {for (i = 1; i <= NF; i++) print i, $i}' file prints every field of the first line with its number. In the sample combined log, $1 is the client IP, $7 the path, $9 the status, and $10 the response size.

When --help is unsupported, use man awk; in restricted environments such as HackerRank, awk --help 2>&1 | less.3

uniq

uniq (from “unique”) combines identical lines only when they are adjacent; it has no memory of earlier lines. That is why it almost always comes right after sort.

OptionEffect
-cPrefix each line with its count
-dShow only repeated lines
-uShow only lines that occur once
-iIgnore case

As listed in the note. If you only need sorted, deduplicated output, sort -u does both. The macOS/BSD version may not support --help; use man uniq.2

Footnotes

  1. Bash SRE HackerRank Quick Reference, original ↩ ↩2 ↩3

  2. The uniq command, original ↩ ↩2

  3. The awk command, original ↩ ↩2 ↩3 ↩4