Related commands:
comm — Compare two sorted files line by line. join — Join the lines of two files which share a common field of data. uniq — Identify, and optionally filter out, repeated lines in a file.
sort is a simple and very useful command which will rearrange the lines in a text file so that they are sorted, numerically and alphabetically.
Sort syntax:
sort [OPTION]... [FILE]...
By default, the rules for sorting are:
The rules for sorting can be changed according to the options you provide to the sort command.
You can also use the built-in sort option -o, which allows you to specify an output file:
sort -o output.txt data.txt
You can perform a reverse-order sort using the -r flag. For example, the following command:
sort -r data.txt
You can sort numbers using the -n option:
sort -n data.txt
You can sort by the second field on each line of input sort -k 2
Parts of fields can be further specified with “-k n.m“, says the man page.
sort -k 2.3
should sort by the second field, starting with the third character in that field. But getting the -k x.y
notation to work is tricky and for it to work just add the -b
flag. For more refer to: http://unixetc.co.uk/2012/11/13/sorting-with-k-on-unix-and-linux