Linux sorting command sort-Linux general technology-Linux technology and application information. The following is a detailed description. 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 field position starts from posl to pos2 (including posl, excluding 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 spaces (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
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.
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.