sort and remove duplicate lines
Sort lines, keep each once, count how often each appears.
Steps
-
sort file.txtsort the lines
-
sort -u file.txtsort them and keep each once
-
sort file.txt | uniq -c | sort -rnhow often each line appears, most first
-
awk '!seen[$0]++' file.txtkeep each once, in their order
-
sort -t, -k3 -n data.csvsort a CSV by its third column, as numbers
-
sort -h sizes.txtsort sizes like 2K, 1M, 3G
Notes
- uniq only removes repeats next to each other: sort first.
Commands used
sortuniqawk
Each has a page in lore: lore sort opens it.
Related recipes
- add up numbers in a fileSum a column of numbers, from a file or a command's output.
- compare two foldersFind which files differ between two folders, or are only in one of them.
- count lines of codeHow many lines of code a project has, in total and by language.
- count lines, words and filesCount the lines of a file or of a command's output, the words, and the files in a folder.