1 How the Sort works
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.
[email protected] rh]$ cat Seq.txtbananaapplepearorangepear[[email protected] rh]$ sort Seq.txt Applebananaorangepearpear
2-u option for sort
It is simple to remove duplicate rows in the output line.
[Email protected] rh]$ sort-u seq.txt applebananaorangepear
Pear was ruthlessly removed by the-u option because of repetition.
3 Sort's-r option
[email protected] rh]$ cat Number.txt1357112461089[[email protected] rh]$ sort Number.txt-- Sort By default is ascending 1101123456789[[email protected] rh]$ sort-n number.txt--The sorting program sorts the numbers by character, the sorting program compares 1 and 2, apparently 1 is small, so 10 is placed before 2 Polygon 1234567891011[[email protected] rh]$ sort-n-r number.txt --r = descending, n means sort by number 1110987654321
4-o option for sort
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.
Number is cleared. So we need to use the-o option, 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 specific direction.
[[email protected] rh]$ Sort-n-R number.txt-o number.txt[[email protected] rh]$ cat Number.txt 1110987654321
5 Sort's-t option and-K option
[email protected] rh]$ cat Facebook.txtbanana:30:5.5apple:10:2.5pear:90:2.3orange:20:3.4[[email protected] rh]$ sort -n-k 2-t: facebook.txtapple:10:2.5orange:20:3.4banana:30:5.5pear:90:2.3
This 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 would like to sort by the number of fruits, that is, in the second column, how to use the sort implementation? Fortunately, sort provides the-t option, after which you can set the spacer. (Does not think of the cut and paste's-D option, resonance ~ ~)
After you specify a spacer, you can use-K to specify the number of columns. We use the colon as the spacer, and we sort the numbers in ascending order for the second column, and the result is very satisfying.
6 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.
The sort ordering command in Linux (i)