uniq Linux Command
Reports or filters out repeated lines in a sorted file.
What does uniq do?
Reports or filters out repeated lines in a sorted file.
Syntax
uniq [options] filePractical example
sort file.txt | uniq -cQuestions about uniq
How do I remove duplicate lines with uniq?
Use `sort file | uniq` to remove duplicates. uniq only removes consecutive duplicates, so the file must be sorted first.
How do I count occurrences with uniq?
Use `sort file | uniq -c` to count how many times each line appears. The count is prepended to each line.
How do I show only duplicate lines?
Use `sort file | uniq -d` to display only lines that appear more than once.
How do I show only unique lines?
Use `sort file | uniq -u` to display only lines that appear exactly once (no duplicates).
How do I ignore case when finding duplicates?
Use `sort file | uniq -i` to ignore case. 'Hello' and 'hello' will be treated as duplicates.
How do I skip fields when comparing lines?
Use `uniq -f 2 file` to skip the first 2 fields when comparing lines. Useful for ignoring timestamps or prefixes.
How do I count unique words in a file?
Use `cat file | tr ' ' '\n' | sort | uniq -c` to split words, sort them, and count occurrences.
How do I sort and count log entries?
Use `sort access.log | uniq -c | sort -rn` to count and sort log entries by frequency, most common first.
Related Search Commands
sortSorts lines of text files alphabetically or numerically.
cutExtracts specific columns/fields from each line of a file.
sedStream editor for filtering and transforming text (find/replace, insert, delete).
trTranslates, deletes, or squeezes characters from standard input.
awkPowerful text processing language for pattern scanning and data extraction.
xargsBuilds and executes command lines from standard input (useful with pipes).