Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Linux sort Command

Linux sort command

Linux Command Reference Linux Command Reference

The Linux sort command is used to sort the contents of a text file.

sort can sort the contents of a text file line by line.

Syntax



sort [-bcdfimMnr][-o<输出文件>][-t<分隔字符>][+<起始栏位>-<结束栏位>][--help][--verison][文件][-k field1[,field2]]

Parameter Description:

Example

To sort the lines of a file with the sort command using the default method, use the following command:



sort testfile 

The sort command sorts the first column of a text file in ASCII order by default and outputs the result to standard output.

Using the cat command to display the testfile shows its original order as follows:



$ cat testfile      # testfile文件原有排序  

test 30  

Hello 95  

Linux 85 

The result after reordering with the sort command is as follows:



$ sort testfile # 重排结果  

Hello 95  

Linux 85  

test 30 

Using the -k parameter to sort by the values in the second column gives the following result:



$ sort testfile -k 2

test 30  

Linux 85 

Hello 95  

Linux Command Reference Linux Command Reference