The uniq command
report or omit repeated lines 11 examples from the lessons of the IVski Linux Playground — try them in the terminal next to them.
Options in the examples
-c, --count- prefix lines by the number of occurrences
-d, --repeated- only print duplicate lines, one for each group
-u, --unique- only print unique lines
Examples
sort file.txt | uniqRemove duplicates
sort file.txt | uniq -cCount occurrences
sort file.txt | uniq -dShow only duplicates
sort file.txt | uniq -uShow only unique lines
history | awk '{print $2}' | sort | uniq -c | sort -rn | head -10
cat access.log | awk '{print $1}' | sort | uniq -c | sort -rn
cut -d: -f7 /etc/passwd | sort | uniq -c
awk '{print $1}' /var/log/nginx/access.log | sort | uniq -c | sort -rn | head -3
awk '{print $9}' /var/log/nginx/access.log | sort | uniq -c
awk '$9 == 404 {print $7}' /var/log/nginx/access.log | sort | uniq -c | sort -rn
awk '{print $1}' ~/logs/access.log | sort | uniq -c | sort -rn | head -3Requests per address, most first
Lessons that use uniq
- sort & uniq — Sorting and Deduplication
- cut & tr — Extracting Columns
- Reading the Logs
- Project: Daily Log Report