uniq
In this page:
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
🔒
Chapter Quiz — Complete all 12 topics to unlock
0/12 topics done
Complete these topics first: