Linux sort Command

Linux 命令大全Linux Command Encyclopedia

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

sort can sort the contents of text files line by line.

Syntax

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

Parameter Description:

  • -b Ignore space characters at the beginning of each line.
  • -c Check whether the file has been sorted in order.
  • -d When sorting, ignore characters other than English letters, numbers, and space characters.
  • -f When sorting, treat lowercase letters as uppercase letters.
  • -i When sorting, ignore characters other than ASCII characters between 040 and 176.
  • -m Merge several sorted files.
  • -M Sort the first 3 letters according to month abbreviations.
  • -n Sort according to numerical value.
  • -u Means unique; the output is deduplicated.
  • -o<output file> Store the sorted result in the specified file.
  • -r Sort in reverse order.
  • -t<separator> Specify the field separator used when sorting.
  • +<start field>-<end field> Sort by the specified fields, ranging from the start field to the field before the end field.
  • --help Display help.
  • --version Display version information.
  • [-k field1[,field2]] Sort by the specified columns.

Examples

When using the sort command to sort the lines of a file in the default way, the command used is as follows:

sort testfile 

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

Using the cat command to display the testfile, we can see its original sorting is as follows:

$ cat testfile      # testfile文件原有排序  
test 30  
Hello 95  
Linux 85 

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

$ sort testfile # 重排结果  
Hello 95  
Linux 85  
test 30 

Using-kSetting parameters to rearrange the values in the second column, the result is as follows:

$ sort testfile -k 2
test 30  
Linux 85 
Hello 95  

Linux 命令大全Linux Command Encyclopedia

Other Extensions