Sorting unique and duplicates
Sorting is a common task that we can encounter with text files. The sort
command helps us to perform sort operations over text files and stdin
. Most often, it can also be coupled with many other commands to produce the required output.
uniq
is another command that is often used along with a sort
command. It helps to extract unique (or duplicate) lines from a text or stdin
. This recipe illustrates most of the use cases with sort
and uniq
commands.
Getting ready
The sort
command accepts input as filenames, as well as from stdin
(standard input) and outputs the result by writing into stdout
. The same applies to the uniq
command.
How to do it...
We can easily sort a given set of files (for example,
file1.txt
andfile2.txt
) as follows:$ sort file1.txt file2.txt > sorted.txt
Or:
$ sort file1.txt file2.txt -o sorted.txt
For a numerical sort, we can use:
$ sort -n file.txt
To sort in the reverse order, we can use:
$ sort -r file.txt
For sorting by months (in the order...