Sorting unique and duplicate lines
Sorting text files is a common task. The sort
command sorts text files and stdin
. It can be coupled with other commands to produce the required output. uniq
is often used with sort
to extract unique (or duplicate) lines. The following recipes illustrate some sort and uniq
use cases.
Getting ready
The sort
and uniq
commands accept input as filenames or from stdin
(standard input) and output the result by writing to stdout
.
How to do it...
- We can sort a set of files (for example,
file1.txt
andfile2.txt
), like this:
$ sort file1.txt file2.txt > sorted.txt
Alternatively, use this:
$ sort file1.txt file2.txt -o sorted.txt
- For a numerical sort, we use this:
$ sort -n file.txt
- To sort in the reverse order, we use the following command:
$ sort -r file.txt
- To sort by months (in the order Jan, Feb, March,...), use this:
$ sort -M months.txt
- To merge two already sorted files, use this command:
$ sort -m sorted1 sorted2
- To find the unique lines from a sorted file, use this:
$...