YouTip LogoYouTip

Linux Comm Sort

Linux sort Command

Linux sort Command

Linux sort Command

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 [-o<output_file>][-t<separator>][+<start_field>-<end_field>][-k field1[,field2]]

Parameter Description:

  • -b Ignore leading blanks.
  • -c Check if the file is already sorted.
  • -d In sorting, ignore characters other than letters, digits, and blanks.
  • -f In sorting, fold lowercase to uppercase.
  • -i In sorting, ignore non-printable characters except ASCII 040 to 176.
  • -m Merge already sorted files.
  • -M Sort the first three letters as month abbreviations.
  • -n Sort numerically.
  • -u Unique. Output only the first of an equal run.
  • -o<output_file> Write result to output_file instead of standard output.
  • -r Reverse the result of comparisons.
  • -t<separator> Use separator as the field separator.
  • +<start_field>-<end_field> Sort by a range of fields, from start_field to the field before end_field.
  • --help Display help.
  • --version Display version information.
  • [-k field1[,field2]] Sort by a specific column.

Examples

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

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 shows its original order as follows:

$ cat testfile # Original order of testfile
test 30
Hello 95
Linux 85

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

$ sort testfile # Sorted result
Hello 95
Linux 85
test 30

Using the -k parameter to sort by the value of the second column gives the following result:

$ sort testfile -k 2
test 30
Linux 85
Hello 95
← Linux Comm SpellLinux Comm Sed β†’