Linux Sorting
Sorting
Section titled “Sorting”Sorts lines in one or more files alphabetically (default) or numerically (-n).
sort input1.txt input2.txt > output.txtWrites the sorted content of both files into output.txt.
Useful Options
Section titled “Useful Options”-n→ numeric sort-r→ reverse order-u→ unique (equivalent tosort | uniq)-k→ sort by column/field (e.g.,sort -k2 file.txt)-t→ specify delimiter (e.g.,sort -t, -k2 file.csv)
Removes adjacent duplicate lines (so usually combined with sort).
sort input.txt | uniq > output.txtProduces a sorted file without duplicates.
Useful Options
Section titled “Useful Options”-c→ prefix lines by occurrence count-d→ show only duplicates-u→ show only unique lines
Example:
sort names.txt | uniq -c | sort -nrCounts duplicate entries, then sorts results by frequency.
Comparison
Section titled “Comparison”| sort | Order lines alphabetically/numerically | sort -n scores.txt |
|---|---|---|
| uniq | Remove duplicate adjacent lines | sort data.txt | uniq |