Refer to the sort usage of Linux
This article is actually mainly recorded in tab-delimited cases
The sort command is to help us sort by different data types, its syntax and common parameter formats:
Sort [-bcfmnrtk][source file][-o output file]
Additional notes: Sort can be sorted for the content of the text file, in the unit of behavior.
Parameters
-B ignores whitespace characters that begin before each line.
-C checks whether the files are sorted in order.
-f sort, ignores uppercase and lowercase letters.
-M sorts the first 3 letters according to the abbreviation of the month.
-N Sorts by the size of the numbers.
-o < output file > The sorted result is saved in the specified file.
-R is sorted in reverse order.
-T < delimited characters > specifies the field separator character to use when sorting.
-K selects which interval to sort.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Here are some examples of how Sort is used.
(1) Sort compares each line of a file as a unit, comparing it from the first character backwards, to the ASCII value in turn, and finally outputting them in ascending order.
Cat seq sortseq. Txtapplebananaorangepear
The user can save the sorted file contents, or output the sorted file contents to the printer. In the following example, the user saves the sorted file contents to a file named result.
$ Sort seq.txt > result
(2) Sort-u option
It is simple to remove duplicate rows in the output line.
Cat seq sortseqsortseq. Txtapplebananaorangepear
Pear was ruthlessly removed by the-u option because of repetition.
(3) Sort-r option
The sort by default is in ascending order, and if you want to change to a descending order, add-R is done.
[Email protected] programming]$CatNumber.txt13524[email protected] programming]$SortNumber.txt12345[email protected] programming]$Sort-R Number.txt54321
(4) Sort-o option
Because sort defaults to outputting the results to standard output, a redirect is required to write the results to a file, such as the sort filename > NewFile.
However, if you want to output the sorting results to the original file, redirection is not possible.
[Email protected] programming]$ sort-r number.txt > Number.txt
[email protected] programming]$ cat Number.txt
[Email protected] programming]$
Look, the number was emptied.
At this point, the-O option appears, which successfully solves this problem, allowing you to confidently write the results to the original file. This may also be the only advantage of the O-gravity orientation.
Cat Number.txt 1 3 5 2 4 Sort -R number.txt-cat number.txt543 21
(5) The Sort-n option
Have you ever encountered 10:2 small cases. I've met anyway. This occurs because the sorting program sorts the numbers by character, and the sorting program compares 1 and 2, which is obviously 1 small, so 10 is placed before 2. This is also the sort's consistent style.
If we want to change this situation, we need to use the-n option to tell sort, "Sort by value"!
[Email protected] programming]$CatNumber.txt1Ten + One25[email protected] programming]$SortNumber.txt1Ten One +25[email protected] programming]$Sort-N number.txt125Ten One +
(6) Sort of-t option and-K option
If there is a file with the contents of this:
[Email protected] programming]$CatFacebook.txtbanana: -:5.5Apple:Ten:2.5Pear: -:2.3Orange: -:3.4This file has three columns, separated by a colon between the column and column, the first column indicates the fruit type, the second column represents the fruit quantity, and the third column represents the fruit price. So I want to sort by the number of fruits, that is, in the second column, how to useSortRealize?
FortunatelySortThe-t option is provided, after which you can set the spacer. After you specify a spacer, you can use-k To specify the number of columns. [Email protected] programming]$Sort-n-k2-t ': ' Facebook.txtapple:Ten:2.5Orange: -:3.4Banana: -:5.5Pear: -:2.3
Note here that if you are tab-delimited, you can do this:
$Sort'\ t' -k3,3n a.txt >a.sortsort: Multi-character tab ' \ t ' (Error)
This cannot be sorted by the tab delimiter, in the following two ways.
1sort -T $'t' -k3,3n a.txt>a.sort2 Sort -t'<ctrl>v<tab>' -k3,3n a.txt>a. Sort<ctrl>v<tab > means press the CTRL and V keys at the same time, then release and press the TAB key.
The 1th kind of operation is OK, the 2nd type can only operate in the terminal, not in the script.
(8) Other sort common options
-F converts lowercase letters to uppercase for comparison, i.e. ignores case
-C checks to see if the file is ordered, and if it is out of order, it outputs the information about the first scrambled row, and finally returns 1
-C Checks if the file is ordered, if it is not output, returns only 1
-M is sorted by month, for example, JAN is less than FEB and so on
-B ignores all the blank parts preceding each line, starting with the first visible character.
Linux Sort Usage