1. How sort works
Sort compares each row of a file as a unit. The comparison principle is to compare the lines from the first character to the back, compare them by ASCII code values, and output them in ascending order.
# cat file.txtbananaapplepearorangepear
2. Sort-u option: Remove duplicate rows in the output row
# sort -u file.txtapplebananaorangepear
3. Sort-r option: Descending
# sort -r file.txtpearorangebananaapple
4. Sort-O option: Redirection File
# sort -r file.txt -o sorted.txt
5. Sort-N option: sorted by numbers
# cat number.txt51101243# sort -n number.txt13451012
6. Sort's-T option and-K Option
# cat fruit.txtbanana:30:5.5apple:10:2.5pear:90:2.3orange:20:3.4
The preceding file contains 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?
Sort provides the-T option, and you can set the delimiter later. (Do you think of the-D option of cut and paste, and resonate ~~)
After the Delimiter is specified, you can use-K to specify the number of columns.
# sort -n -k 2 -t : fruit.txtapple:10:2.5orange:20:3.4banana:30:5.5pear:90:2.3
Note the following when using the-K option:
# Sort-n-k 2-K 3-T: fruit.txtapple:10:2.5orange:20:3.4banana:30:5.5pear:90:2.3
-K can be used. The upper column command means that when the column is sorted by the second column, if the column is the same, it will be sorted by the third column. The default value is the first column.
# sort -k 1.2,1.2 -t : fruit.txtbanana:30:5.5pear:90:2.3apple:10:2.5orange:20:3.4
Because only the second letter is sorted, we use the expression-k1.2 and 1.2, indicating that we only sort the second letter. (If you ask "How can I use-k1.2 ?", Of course not, because you omit the end part, which means that you will sort the strings from the second letter to the last character of the domain ).
7. Sort-F option: case-insensitive
This command is easy to describe.