Linux sort command

Source: Internet
Author: User

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.txt
apple: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.

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.