uniq: remove and count duplicate lines
uniq removes repeated lines, or counts them. It only compares each line with the one before it, so you almost always sort the input first.
sort file | uniq [options]
Common options
| Option | Meaning |
|---|---|
-c | Prefix each line with the number of times it occurred |
-d | Only print lines that are repeated |
-u | Only print lines that are not repeated |
-i | Ignore case |
-f N | Ignore the first N fields when comparing |
Examples
sort list.txt | uniq # unique lines (same as sort -u)
sort list.txt | uniq -c | sort -rn # how often each line occurs, most common first
sort emails.txt | uniq -d # e-mail addresses that appear more than once
sort a.txt b.txt | uniq -u # lines in only one of the two files (if neither file repeats lines itself)
awk '{print $9}' access.log | sort | uniq -c # count HTTP status codes
grep -o "user=[a-z]*" app.log | sort | uniq -c | sort -rn | head
Without sorting
Unsorted input only loses adjacent duplicates:
$ printf 'a\na\nb\na\n' | uniq
a
b
a
To remove duplicates but keep the original order, use awk instead: awk '!seen[$0]++' file. See awk.