uniq Command Reference for CI Scripts
uniq collapses or counts consecutive identical lines.
uniq is deceptively simple: it only looks at adjacent lines, so it must follow sort to deduplicate a whole file. Forgetting that silently lets duplicates through.
Common flags/usage
- -c / --count: prefix each line with its count
- -d / --repeated: print only duplicated lines
- -u / --unique: print only non-duplicated lines
- -i / --ignore-case: case-insensitive comparison
- -f N / -s N: skip first N fields / chars when comparing
Example
shell
awk '{print $1}' access.log | sort | uniq -c | sort -rn | head
sort emails.txt | uniq -d # show only duplicatesIn CI
The number-one mistake is running uniq on unsorted input, so duplicates spread across the file survive; always sort first or use sort -u. For counting unique values regardless of order, sort | uniq -c is the canonical idiom for log frequency reports.
Key takeaways
- uniq only collapses adjacent lines, so sort the input first.
- sort | uniq -c is the canonical idiom for frequency counts.
- -d shows only duplicates; -u shows only lines that appear once.
Related guides
sort Command Reference for CI Scriptssort orders lines of text in CI for stable diffs. Reference for -n, -u, -k, -t, and -r, plus the locale gotch…
cut Command Reference for CI Scriptscut extracts columns by field or character in CI. Reference for -f, -d, and -c, plus the single-delimiter and…
awk Command Reference for CI Scriptsawk is a pattern-scanning text processor for CI. Reference for -F field separator, -v variables, and $1/$NF f…
wc Command Reference for CI Scriptswc counts lines, words, and bytes in CI. Reference for -l, -w, -c, and -m, plus the leading-whitespace gotcha…