What does `uniq` do, and why do you almost always sort first?
BeginnerConceptText processing: grep, sed, awk, cut, sort, uniq
Answer
uniq removes or counts adjacent duplicate lines. Without sorting, a b a stays a b a.
sort ips.txt | uniq -c | sort -rn | head # top talkers
sort list.txt | uniq -d # only duplicated lines
sort list.txt | uniq -u # only lines that appear once
sort -u is enough for plain de-duplication; use uniq -c when you need counts.
More on Text processing: grep, sed, awk, cut, sort, uniq
What are the most useful `grep` flags?BeginnerWhat is the difference between basic, extended and Perl regular expressions in grep?IntermediateWhat are the core `sed` commands you use in automation?BeginnerWhy does `sed -i` behave differently on macOS and Linux?IntermediateHow do you use variables inside a `sed` expression safely?AdvancedWhat is the structure of an `awk` program?Beginner