The Sort command sorts all rows in the file. The Sort command has many very useful options, which were initially used to Sort the file content in the database format. In fact, the Sort command can be considered as a very powerful data management tool to manage files with similar content to database records.
The Sort command sorts the content in the file line by line. If the first character of the two lines is the same, the command will continue to compare the next character of the two lines, the comparison will continue.
Syntax:
Sort [Option] <File>
Note: The Sort command sorts all the rows in the specified file and displays the results on the standard output. If the input file is not specified or "-" is used, the sorting content comes from the standard input.
Sort sorting is done by comparing one or more keywords extracted from the input line. The sorting keyword defines the smallest character sequence used for sorting. By default, the entire behavior keyword is ordered by ASCII characters.
The options for changing the default settings are as follows:
-M if the given file is sorted, merge the file.
-C check whether the given files are sorted in order. If they are not all sorted, an error message is printed and the system exits with the status value 1.
-U considers that only one row is left for the same row after sorting.
-O: the output file writes the sorting output to the output file instead of the standard output file. If the output file is one of the input files, Sort first writes the content of the file to a temporary file, then sort and write the output results.
The options for changing the default sorting rule are as follows:
-D is ordered alphabetically. Only letters, numbers, spaces, and tabs are meaningful for comparison.
-F treats lowercase letters as uppercase letters.
-I ignore non-printable characters.
-M as the month comparison: "JAN" <"FEB"
-R outputs the sorting results in reverse order.
+ Posl-pos2 specifies one or more fields as the sorting keyword. The position of the field starts from posl and ends with pos2 and does not include pos2 ). If pos2 is not specified, the keyword is from posl to the end of the row. The position of the field and character starts from 0.
-B ignores leading blank spaces and tabs when searching for sorting keywords in each line ).
-T separator specifies the character separator as the field separator.
The following describes how to use Sort through several examples.
Use the Sort command to Sort all rows in the text file and output the result. Note that the first word on the second and third lines of the original file is exactly the same. This command will continue to compare the second word vegetables with the first character of fruit.
$ Cat text
Vegetable soup
Fresh vegetables
Fresh fruit
Lowfat milk
$ Sort text
Fresh fruit
Fresh vegetables
Lowfat milk
Vegetable soup
You can save the sorted file content or output the sorted file content to the printer. In the following example, the user saves the sorted file content to the file named result.
$ Sort text> result
Sort the content of the file example using 2nd fields as the sorting keyword.
$ Sort + 1-2 example
For file1 and file2 files, the results are placed in outfile, and the first character of the 2nd fields is used as the sorting keyword.
$ Sort-r-o outfile + 1.0-1.1 example
Sort sorting is often used in pipelines with other commands to combine complex functions, such as sending files in the current working directory to Sort for sorting, the sorting keywords are 6th to 8th fields.
$ Ls-l | Sort + 5-7
$ Ps-e-o "comm pid time" | Sort-d // Sort by the alphabetic order of the first letter of the command
The Sort command can also operate on standard input. For example, if you want to merge several file text lines and sort the merged text lines, you can first use the command cat to merge multiple files, then, use the pipeline operation to input the merged text lines to the command Sort. The Sort command will output the merged and sorted text lines. In the following example, the text lines of the file veglist and file fruitlist are merged and sorted and saved to the file clist.
$ Cat veglist fruitlist | Sort> clist
The article ends.
Related Articles]
- How to Use RPM command parameters in Linux
- Ping Command background process and analysis of returned information
- Cleverly use the ping command to check network faults