Linux sort command - Sort lines of text files
Himanshuz.chd 270004408M Visits (151887)
man page of sort command:
So we see that the main purpose of this command is to produce a sorted output.
Linux sort command examples
1. A basic exampleThe very first input that I tried consisted of some random alphabets.
Here is what I tried:
$ sort b z a w sAnd here is the output :
a b s w zSo we see that the output produced was in sorted form.
2. Sort numbersIn the following example, I filled a text file (sort.txt) with some random numbers.
$ cat sort.txt 8 2 6 1 5 3Then I used the sort command with sort.txt as input file to the command.
$ sort sort.txt 1 2 3 5 6 8So we see that sorted list of numbers was produced in output.
3. Sorting wordsIn this example, the sort.txt file is filled with some words.
$ cat sort.txt UK Australia Newzealand Brazil AmericaNow, this file is given as input to the sort command:
$ sort sort.txt America Australia Brazil Newzealand UKSo we see that words were sorted according to dictionary ordering. Even the words beginning with same alphabet were sorted according to succeeding alphabets.
4. Use sort to directly write data in sorted mannerThis command can be used to write unsorted input data to a file directly in sorted manner.
Here is how this can be done :
$ sort > sort.txt 9 Hello 4 Why 8 ByeAfter the above operation, let's check the file contents :
$ cat sort.txt 4 8 9 Bye Hello WhySo the output suggests that the input was first sorted and then written to file.
5. Write sorted concatenation of all input files to standard outputIf more that one file is provided as input, the sort command produces a sorted concatenation on stdout.
Here is an example:
$ cat sort1.txt 7 4 9 1
$ cat sort2.txt 8 5 6 2Here is the output :
$ sort sort1.txt sort2.txt 1 2 4 5 6 7 8 9So we see that a sorted concatenation was produced in output.
6. Write result of sort in a fileThe output of sort command can be written to a file by using -o option.
Here is how it's done :
$ sort -o sort.txt 4 9 2 8 1Now let's check the file :
$ cat sort.txt 1 2 4 8 9So we see that the output was actually written to the file whose name was supplied as input to sort through -o option.
7. Sort monthsThere is an interesting option -M through which the month names can be sorted.
Here is an example :
$ sort -M > sort.txt DEC JAN FEBNow, let's check the file contents for output :
$ cat sort.txt JAN FEB DECSo we see that sort command actually sorted the month names.
8. Sort human readable numbersAnother interesting option -h is provided by sort command through which human readable numbers.
Here is an example :
$ sort -h > sort.txt 2G 1K 3MNow, let's check the file for output:
$ cat sort.txt 1K 3M 2GSo we see that the numbers were sorted.
9. Produce reverse sorted resultsUsing -r option provided by sort command, the results can be produced in reverse order.
$ sort -h -r > sort.txt 2G 1K 3MHere is the output of file :
$ cat sort.txt 2G 3M 1KSo we see that this time the sorting results were written in reverse sorted order.
10. Compare according to string numerical valueThis can be done using -N option.
Here is the input :
$ cat > sort.txt 7 mangoes 4 oranges 9 grapes 1 appleHere is the output :
$ sort -n sort.txt 1 apple 4 oranges 7 mangoes 9 grapes