-
Book Overview & Buying
-
Table Of Contents
Linux Shell Scripting Cookbook, Second Edition - Second Edition
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.
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.
We can easily sort a given set of files (for example, file1.txt and file2.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...
Change the font size
Change margin width
Change background colour