The sort command is used to sort data based on different data types. Its syntax and common parameter formats are as follows:
Sort [-bcfmnscn] [source file] [-O output file]
Note: Sort can sort the content of text files by unit of action.
Parameters:
-B ignores the leading space characters in each line.
-C: Check whether the files are sorted in order.
-F indicates that uppercase and lowercase letters are ignored during sorting.
-M sorts the first three letters by the abbreviation of the month.
-N is sorted by the value size.
-O <output file> stores the sorted results to the specified file.
-R is sorted in reverse order.
-T <delimiter> specifies the column delimiter used for sorting.
-K: select the range in which the data is sorted.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
The following describes how to use sort through several examples.
(1) Sort compares each row of the file as a unit. The comparison principle is to compare the lines from the first character to the back, compare them by ASCII code value, and finally output them in ascending order.
[Rocrocket @ rocrocket programming] $ cat seq.txt
Banana
Apple
Pear
Orange
[Rocrocket @ rocrocket programming] $ sort seq.txt
Apple
Banana
Orange
Pear
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 seq.txt> result
(2) sort-u Option
The function is very simple, that is, to remove duplicate rows in the output row.
[Rocrocket @ rocrocket programming] $ cat seq.txt
Banana
Apple
Pear
Orange
Pear
[Rocrocket @ rocrocket programming] $ sort seq.txt
Apple
Banana
Orange
Pear
Pear
[Rocrocket @ rocrocket programming] $ sort-u seq.txt
Apple
Banana
Orange
Pear
Pear is repeatedly deleted by the-u option.
(3) sort-r Option
The default sorting method of sort is ascending. If you want to change it to descending order, add-R.
[Rocrocket @ rocrocket programming] $ cat number.txt
1
3
5
2
4
[Rocrocket @ rocrocket programming] $ sort number.txt
1
2
3
4
5
[Rocrocket @ rocrocket programming] $ sort-r number.txt
5
4
3
2
1
(5) sort-O options
Because sort outputs the result to the standard output by default, you need to use redirection to write the result to a file, such as sort FILENAME> newfile.
However, if you want to output the sorting result to the original file, you cannot use redirection.
[Rocrocket @ rocrocket programming] $ sort-r number.txt> number.txt
[Rocrocket @ rocrocket programming] $ cat number.txt
[Rocrocket @ rocrocket programming] $
Check that the number is cleared.
At this point, the-O option appears. It successfully solves this problem and allows you to write the result to the original file with confidence. This is perhaps the only advantage of-O proportional targeting.
[Rocrocket @ rocrocket programming] $ cat number.txt
1
3
5
2
4
[Rocrocket @ rocrocket programming] $ sort-r number.txt-O number.txt
[Rocrocket @ rocrocket programming] $ cat number.txt
5
4
3
2
1
(6) sort-N Option
Have you ever encountered 10 to 2 problems. I have encountered it. This is because the sorting program sorts these numbers by characters. The sorting program will first compare 1 and 2, obviously 1 is small, so it will put 10 in front of 2. This is also the consistent style of sort.
If we want to change this situation, we need to use the-n option to tell sort, "sort by value "!
[Rocrocket @ rocrocket programming] $ cat number.txt
1
10
19
11
2
5
[Rocrocket @ rocrocket programming] $ sort number.txt
1
10
11
19
2
5
[Rocrocket @ rocrocket programming] $ sort-N number.txt
1
2
5
10
11
19
(7) Sort's-T option and-K Option
If the content of a file is as follows:
[Rocrocket @ rocrocket programming] $ cat facebook.txt
Banana: 30: 5.5
Apple: 10: 2.5
Pear: 90:2.3
Orange: 20: 3.4
This file has three columns separated by colons. The first column indicates the fruit type, the second column indicates the fruit quantity, and the third column indicates the fruit price. So I want to sort by the number of fruits, that is, by the second column. How can I use sort to achieve this? Fortunately, sort provides the-T option, and you can set the delimiter later. After the Delimiter is specified, you can use-K to specify the number of columns.
[Rocrocket @ rocrocket programming] $ sort-n-k 2-t': 'facebook.txt
Apple: 10: 2.5
Orange: 20: 3.4
Banana: 30: 5.5
Pear: 90:2.3
(8) other common sort options
-F converts lowercase letters to uppercase letters for comparison, that is, the case is ignored.
-C checks whether the file is sorted out. If the file is in disordered order, information about the first unordered row is output, and 1 is returned.
-C checks whether the file is sorted out. If the file is not output in disordered order, only 1 is returned.
-M is sorted by month, for example, Jan is smaller than Feb.
-B ignores all the blank parts in front of each line and compares them from the first visible character.