Sort (not excerpted)

Source: Internet
Author: User
The sort command sorts text lines and prints the files to the screen.
The sort command provides a way to quickly organize data in alphabetical or numerical order. By default, the sort command uses white space to separate different fields in the file.
Sort files, merge sorted files, and check files to determine whether they are sorted.

The sort command sorts the rows in the file specified by the file parameter and writes the result to the standard output. If the file parameter specifies multiple files, the sort command connects these files and sorts them as one file. -(Minus sign) specifies the standard input instead of the file name. If no file name is specified, this command sorts the standard input. You can use the-O flag to specify the output file.
If no flag is specified, the sort command sorts all rows in the input file based on the sorting order in the current language environment.
When the sort keyword is used, the sort command First sorts all rows based on the content of the first sort keyword. Then, sort all rows with the same first sort keyword based on the content of the second sort keyword. Number the sorting keywords in the order they appear in the command line. If the two rows sort all the sorting keywords in the same order, compare all rows according to the sorting order in the current language environment.

Main Parameters

-A uses the ASCII sorting order instead of the current language environment sorting order on a byte basis.
-B ignores leading spaces and tabs to find the first or last column of the field.
-C. Check whether the input has been sorted by the sorting rules specified in the flag. If the input file is incorrectly sorted, a non-zero value is returned.
-D. Sort data in alphabetical order. Only letters, numbers, and spaces are considered in the comparison.
-F replace all lowercase letters with uppercase letters before comparison.
-I Comparison ignores all non-display characters.
-K keydefinition specifies the sorting keyword. The format of the keydefinition option is:
[Fstart [. cstart] [modifier] [, [fend [. Cend] [modifier]
The sorting keyword includes all the characters starting with the fields specified by the fstart variable and the columns specified by the cstart variable, and the fields specified by the Fend variable and
The ending character of the column specified by the Cend variable. The value of the modifier variable can be B, d, f, I, n, or R. The modifier is equivalent to a flag of the same letter.

-M: only multiple input files are merged. Assume that the input files are sorted.
-N: sorts numeric fields by arithmetic values. The digit field can contain leading spaces, optional minus signs, decimal numbers, thousands separator, and optional base characters.
When you sort fields that contain any non-numeric characters in numbers, unexpected results may occur.
-O outfile directs the output to the file specified by the OUTFILE parameter, instead of the standard output. The OUTFILE parameter value can be the same as the file parameter value.
-R: reverse the specified sorting order.
-T character specifies character as a single field separator.
-U prohibits sorting by sorting keywords and all equivalent options (except for one row in each group ).
-T Directory: Place all temporary files created in the directory specified by the DIRECTORY parameter.
-Y [kilobytes] starts the sort command with the number of kilobytes of the primary storage specified by the kilobytes parameter, and increases the storage capacity as needed.
(If the value specified by the kilobytes parameter is smaller than the minimum storage site or greater than the maximum storage site, it is replaced by the minimum storage site or the maximum storage site ).
If the-y flag is omitted, the sort command starts with the default storage size.
-The y0 flag is started with the minimum storage, while the-y flag (without the kilobytes value) is started with the maximum storage. The storage used by the sort command significantly affects performance.
Sorting large volumes of small files is a waste.
-Z recordsize: if the size of any row being sorted is greater than the default buffer size, it is necessary to prevent exceptional termination.
When the-C or-M flag is specified, the sorting phase is omitted and the default buffer size of the system is used. If the size of the sorted rows exceeds this value, the sorting exception ends.
The-Z option specifies the record of the longest row in the sorting phase, so sufficient buffer can be allocated in the merge phase.
Recordsize must specify the byte value equal to or greater than the maximum row to be merged.

[Example]
(Internet said, in lc_all, lc_collate or Lang environment variables set to en_us in the case of sorting, I see my machine above only lang = zh_CN.UTF-8, after practice found that some sort is messy, set it to Lang = en_us)
Assume that the forsort1 file is as follows:
$ Cat forsort1
Yams: 104
Turnips: 8
Potatoes: 15
Carrots: 104
Green beans: 32
Carrots: 104
Radishes: 5
Lettuce: 15

* Sort the file content by row and output the result to the standard output:
$ Sort forsort1
After the input, the output is as follows:
Carrots: 104
Carrots: 104
Green beans: 32
Lettuce: 15
Potatoes: 15
Radishes: 5
Turnips: 8
Yams: 104
Here, the content of forsort will be rearranged from small to large by rows. If there are multiple keywords (blank by default), start several rows with the same keywords, the sorting will continue by the next keyword.

* Sort the file content in reverse order and output the result to the standard output:
$ Sort-r forsort1
After the input, the output is as follows:
Yams: 104
Turnips: 8
Radishes: 5
Potatoes: 15
Lettuce: 15
Green beans: 32
Carrots: 104
Carrots: 104
Here, we can see that the result is opposite to sort forsort1.

* Sort the file content and output the result to the out.txt file:
$ Sort forsort1-O out.txt
Here, the-O option is used to specify the output file. The result is different from that of sort forsort1, but the result is not output to the standard output and output to the out.txt file.

* Sort. Only one row is retained for the same row:
$ Sort-u forsort1
After the input, the output is as follows:
Carrots: 104
Green beans: 32
Lettuce: 15
Potatoes: 15
Radishes: 5
Turnips: 8
Yams: 104
Here, two carrots rows are deleted.

* Specify the characters of the fields to be separated as ':' and sort by 2nd keywords:
$ Sort-T:-K 2 forsort1
After the input, the output is as follows:
Carrots: 104
Carrots: 104
Yams: 104
Lettuce: 15
Potatoes: 15
Green beans: 32
Radishes: 5
Turnips: 8
Here,-T specifies the separator,-K specifies the location (1 by default), and spaces after-T and-K are optional. If the location is from 2nd to 4th, then this is the case: sort-K2, 4 forsort1

* First sort by 3rd fields, equal and then sort by 4th columns/etc/passwd:
Sort-T:-K3-K4-N/etc/passwd
Here, we need to add-k because we need to compare multiple columns.

* Sort the 2nd fields by number values:
$ Sort-T:-K2-N forsort1
After the input, the output is as follows:
Radishes: 5
Turnips: 8
Lettuce: 15
Potatoes: 15
Green beans: 32
Carrots: 104
Carrots: 104
Yams: 104
Here,-N is sorted by number. If the-n parameter is not added, the sorting result starts with the number on the leftmost side, which is equivalent to the comparison of letters.

Contact Us

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.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.