Sort and Deduplicate a Plain-Text List With sort and uniq
Introduction
To deduplicate a plain-text list in the terminal, run sort -u list.txt. It sorts the lines and keeps one line from each set of equal ones. If you also want to know how often each line repeated, pipe the sorted list into uniq -c.
Tag lists, word lists and reading lists pick up repeats over time. A list with one item per line is exactly the shape these tools expect. Below you'll find both commands, the reason uniq alone often seems to do nothing, and the case and locale details that change the result.
Sort a list and why locale matters
Start with a small file called tags.txt:
notes
markdown
notes
ideas
markdown
notesThe default behavior
sort prints the lines of its input in sorted order. According to the sort entry on Wikipedia), the entire line is the sort key by default. With no file name, or with - as the file name, it reads standard input.
sort tags.txtideas
markdown
markdown
notes
notes
notesThe duplicates now sit next to each other. That detail matters for everything below.
Case, blanks and numbers
A few options change how lines compare:
-ffolds lower case to upper case, soNotesandnotescompare as equal.-bignores leading blanks, so an indented line sorts with its unindented twin.-dconsiders only blanks and alphanumeric characters.-ncompares by numeric value,-Vsorts version numbers naturally, and-rreverses the result.
Sort order is also affected by the environment's locale settings. The same file can come out in a different order on another machine, or in another terminal profile, when uppercase and lowercase letters or accented characters are involved. If you need a result that is identical everywhere, for example in a script, check how your system's locale orders those lines before you rely on the output.
Deduplicate with sort -u
The -u option, long form --unique, suppresses all but one line in each set of lines that have equal keys. One command does both jobs:
sort -u tags.txtideas
markdown
notesThis is a pipeline) in the simplest sense: the output of one command is the input of the next, and the shell joins commands with a vertical bar. You can use sort -u at the end of a longer chain, such as cat a.txt b.txt | sort -u to merge two lists without repeats.
"Equal" depends on the options you pass. Add -f and lines that differ only in case count as equal, so sort -f -u keeps one line for Notes and notes. Add -b and a leading space no longer makes a line unique. Which of the equal lines survives is not something to rely on, so normalize the case in your list first if the exact spelling matters.
Why uniq needs sorted input
uniq outputs its text with adjacent identical lines collapsed to one. The key word is adjacent. Run it on the unsorted file and you get this:
uniq tags.txtnotes
markdown
notes
ideas
markdown
notesNothing changed, because no two neighboring lines were identical. That's why uniq typically runs after sort:
sort tags.txt | uniqideas
markdown
notesFor a plain list, sort -u and sort | uniq give the same lines. The two-command form earns its place when you want the options that only uniq has. Files with one record per line, like the ones in JSON Lines, suit this kind of line-by-line tool for the same reason: each line is a complete item.
Count and find duplicates
uniq -c adds the number of occurrences of each line, and uniq -d prints only the lines that were repeated:
sort tags.txt | uniq -c 1 ideas
2 markdown
3 notessort tags.txt | uniq -dmarkdown
notesTo rank a list by how often each line occurs, add a second sort: sort tags.txt | uniq -c | sort -n. Lines with the lowest counts come first, and sort -rn flips that so the most frequent line leads. It's a quick way to spot a tag you've overused, or a word you keep repeating in a draft.
Save the cleaned list back to a file
The tempting way to overwrite the file is a redirect:
sort -u tags.txt > tags.txtDon't do that. The shell empties tags.txt before sort starts reading it, so you end up with an empty list. Use the -o option, which writes to the output file you name:
sort -u tags.txt -o tags.txtOr write to a new file and compare before replacing the old one:
sort -u tags.txt > tags.clean.txtKeep a copy of the original until you have looked at the result. Once the list is clean, you can count the words in a text file with wc to see how many items you started with and how many remain, or open the file in Carets to read it through before you commit to it.
Conclusion
Sort first, then remove duplicates. Use sort -u when you only need the unique lines, and sort | uniq -c or uniq -d when the repeats are the information you want. Decide up front whether case and leading blanks should count as differences, and remember that locale can change the order of the result.
Frequently Asked Questions
What is the difference between sort -u and sort | uniq?
For plain lines they give the same list of unique lines. sort -u does it in one command by suppressing all but one line in each set of lines with equal keys. Piping to uniq lets you add -c to count occurrences or -d to show only the duplicated lines.
Why does uniq leave duplicates in my list?
uniq collapses only adjacent identical lines. If the duplicates are not next to each other, it leaves them alone. Sort the list first, or use sort -u.
Does sort treat uppercase and lowercase the same?
Not by default, and the order depends on your locale settings. The -f option folds lower case to upper case when comparing, so lines that differ only in case count as equal.