Interview Prep/Shell Scripting/Text processing: grep, sed, awk, cut, sort, uniq

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.

All Shell Scripting questions Roadmap: Shell scripting (Bash)

More on Text processing: grep, sed, awk, cut, sort, uniq