uniq
uniq is like a teacher who crosses out repeated names sitting side by side on a list. It only spots repeats that are next to each other, so you sort first.
In this page:
Syntax
sort file_name | uniq [-c]
uniq
uniq removes consecutive duplicate lines from sorted input, so it's almost always used after sort in a pipeline. -c prefixes each line with a count of how many times it occurred.
Without sorting first, uniq will miss duplicates that aren't adjacent.
Warning:
uniq only removes *adjacent* duplicate lines — always sort first if duplicates might not be next to each other.Example: uniq
#!/bin/bash
printf "apple\nbanana\napple\napple\ncherry\n" | sort | uniq
echo "---"
printf "apple\nbanana\napple\napple\ncherry\n" | sort | uniq -c
Login to try C/C++/Java/PHP code in the editor
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the
view already swaps in the hi_ translation fields (or blanks these
out if untranslated), so this renders correctly for both languages
without a lang_code check here. #}
Common Mistakes
- Using
uniqwithoutsort, since it only removes adjacent duplicates. - Forgetting
-cwhen you want counts. - Expecting
uniqto ignore case without-i.
🔒
Chapter Quiz — Complete all 12 topics to unlock
0/12 topics done
Complete these topics first: