sort command in Linux/Unix
sort command in Linux allows users to sort the content of text files. sort command prints the result in standard output and users can also write standard output to a file.
Syntax for sort command
sort [option] [filename]
To get help with sort command and display all options available with sort command use sort –help.
sort --help
sort –version displays version information of sort command.
sort --version
Mechanism of sort command
- Lines starting with a number will be sorted in ascending order. Lines starting with a number go before the lines beginning with a letter.
- Lines starting with letters will be sorted in ascending alphabetical order. Lines starting with a capital letter (A-Z) go before the lines starting with a small letter (a-z).
Using sort command
- Sorting text file where all the lines start with a capital letter
sort abc.txt
2. Sorting text file where lines start with a capital letter or small letter
sort UpperLower.txt
3. storing output in new text file
sort abc.txt > output.txt
4. alternatively it can also be used to storing output in new text file
sort -o newoutput.txt abc.txt
5. display sort output in reverse order
sort -r abc.txt
sort 123.txt
sort 123abc.txt
8. line starting with number and letter
sort -n 123abc.txt
9. reverse sort output for number
sort -nr 123.txt
sort -g 123abc.txt
11. sort according to first column
sort -k1 pink.txt
12. sort according to second column
sort -k2 pink.txt
sort -M calender.txt
14. check for sorted input; do not sort
sort -c sortc.txt
sort -u sortu.txt
[insert page=’linux-commands-with-example’ display=’content’]