Linux sort command parameters and usage details

  

Function Description: Sort the text file content.

Syntax: sort [-bcdfimMnr][-o<output file>][-t<separation character>][+<starting field>-<end field>] [--help][--verison][file]

Note: Sort can sort the content of text files in units of rows.

Parameters: -b Ignores the space character that begins before each line. -c Checks if the files are sorted in order. -d Sorts, except for English letters, numbers, and space characters, ignoring other characters. -f When sorting, treat lowercase letters as uppercase letters. -i When sorting, except for ASCII characters between 040 and 176, other characters are ignored. -m Combines several sorted files. -M Sorts the first 3 letters according to the abbreviation of the month. -n Sort by size. -o<output file> Saves the sorted result to the specified file. -r Sorts in reverse order. -t<separator character> Specifies the field separator character to use when sorting. +<Start Field>-<End Field> Sort by the specified field, from the start field to the previous field of the end field. --help Displays help. --version Display version information

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

The following uses a few examples to describe the use of Sort.

Use the Sort command to sort the lines in the text file and output the result. Note that the first word on the second and third lines of the original file is identical, and the command will continue to compare from their second word, vegetables, with the first character of the fruit.

$ cat text

vegetable soup

fresh vegetables

fresh fruit

lowfat milk

$ Sort text

fresh fruit

fresh vegetables

lowfat milk

vegetable soup

Users can save sorted files or sort them The contents of the file are output to the printer. In the following example, the user saves the sorted file contents to a file named result.

$ Sort text>result

Sort the contents of the file example with the second field as the sort key.

$ Sort +1-2 example

For the reverse sorting of file1 and file2 files, the result is placed in outfile, using the first character of the second field as the sort key.

$ Sort -r -o outfile +1.0 -1.1 example

Sort sorting is often used in conjunction with other commands in the pipeline to combine more complex functions, such as using the pipeline to the current working directory. The files in the file are sorted by Sort, and the sort key is the 6th to 8th fields.

$ ls - l |  Sort +5 - 7

$ ps -e -o " comm pid time"| Sort -d //Sort alphabetically according to the first letter of the command

The Sort command can also operate on standard input. For example, if you want to merge several lines of text and sort the merged lines of text, you can first merge the multiple files with the command cat and then pipe the merged lines of text into the command Sort. The Sort command will output these merged and sorted lines of text. In the following example, the text line of the file veglist and the file fruitlist are merged and sorted and saved to the file clist.

$ cat veglist fruitlist |  Sort > clist

Copyright © Windows knowledge All Rights Reserved