"Sort helps you"-linux command five-minute series 26

Source: Internet
Author: User
Tags pear

This original article belongs to "Linux greenhouse" blog, the blog address is http://roclinux.cn. The author of the article is rocrocket.

In order to prevent the vicious reproduction of some websites, special in each article before adding this information, but also hope that readers understand.

===

Haven't written technical articles for a long time, sorry everybody. Send the sort for you today to share. :)

[Start of body]

Sort is a very common command in Linux, sort by tube, concentrate, five minutes to fix sort, start now!

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] programming]$ cat Seq.txt
Banana
Apple
Pear
Orange
[Email protected] programming]$ sort Seq.txt
Apple
Banana
Orange
Pear

2-u option for sort

It is simple to remove duplicate rows in the output line.

[email protected] programming]$ cat Seq.txt
Banana
Apple
Pear
Orange
Pear
[Email protected] programming]$ sort Seq.txt
Apple
Banana
Orange
Pear
Pear
[Email protected] programming]$ sort-u seq.txt
Apple
Banana
Orange
Pear

Pear was ruthlessly removed by the-u option because of repetition.

3 Sort's-r option

Sort by default is in ascending order, if you want to change to descending order, add an-R to get it done.

[email protected] programming]$ cat Number.txt
1
3
5
2
4
[Email protected] programming]$ sort Number.txt
1
2
3
4
5
[Email protected] programming]$ sort-r number.txt
5
4
3
2
1

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.

[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 specific direction.

[email protected] programming]$ cat Number.txt
1
3
5
2
4
[Email protected] programming]$ sort-r number.txt-o number.txt
[email protected] programming]$ cat Number.txt
5
4
3
2
1

5-N option for sort

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]$ cat Number.txt
1
10
19
11
2
5
[Email protected] programming]$ sort Number.txt
1
10
11
19
2
5
[Email protected] programming]$ sort-n number.txt
1
2
5
10
11
19

6 Sort's-t option and-K option

If there is a file with the contents of this:

[email protected] 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 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.

[Email protected] programming]$ sort-n-K 2-t: Facebook.txt
apple:10:2.5
orange:20:3.4
banana:30:5.5
pear:90:2.3

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.

7 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.

"Sort helps you"-linux command five-minute series 26

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.