The confusion of Linux sort-g

Source: Internet
Author: User

The sort command helps us sort by different data types and is very efficient to sort, so it's also a very common command.

Sort reference and Description: https://www.gnu.org/software/coreutils/manual/html_node/sort-invocation.html

This paper mainly discusses the confusion caused by using g when sorting.

In order to sort a file, we often sort by several columns in the file.

20160722 817657 EUR EUR. L LIBOR M 6 1.53399856212991e-006
20160722817657EUREUR. lliborm91.73682111461871e-006
20160722817657USDUSD. lbasisoism24-1.23799103559608e-010
20160722817657USDUSD. lbasisoism36-1.61320184994397e-010

To sort the above data, we used a sort command like this: sort-t $ ' \ t ' k 2,2g-k 3,3g-k 4,4-k 5,5-k 6,6-k 7,7-f

As for why using G, in the data column can also see the sorting scientific notation (e) when using n ordering is not accurate, generally speaking, n,g sort is not a problem, for versatility consider the general character to be sorted using G.

According to the sort key, according to a tab-delimited column, sorted by G 2, 3 columns, other default sort, then the expected result is:

20160722 817657 EUR EUR. L LIBOR M 6 1.53399856212991e-006
20160722817657EUREUR. lliborm91.73682111461871e-006
20160722817657USDUSD. lbasisoism24-1.23799103559608e-010
20160722817657USDUSD. lbasisoism36-1.61320184994397e-010

But the result is:

20160722817657USDUSD. lbasisoism24-1.23799103559608e-010
20160722817657USDUSD. lbasisoism36-1.61320184994397e-010

20160722 817657 EUR EUR. L LIBOR M 6 1.53399856212991e-006
20160722817657EUREUR. lliborm91.73682111461871e-006

This is why, on Google for a long time did not find a similar problem, on the StackOverflow experienced people try my sortkey to sort, someone sort is normal.

I try to use N and the default sort is also normal, but with G appears is not the expected results. In order to exclude other factors, all the factors that do not affect are removed.

Let's sort the following data:

Abc
EUR
USD
0
0.000
0.000000000001
0.1
0.0000000000000000000000000000000000000000000000000000
EW

Using SortKey: sort-k 1,1g-f

Abc
USD
0
0.000
0.0000000000000000000000000000000000000000000000000000
EUR
EW
0.000000000001
0.1

This is obviously not what we expected, we will use SortKey: sort-k 1,1-f

0
0.000
0.0000000000000000000000000000000000000000000000000000
0.000000000001
0.1
Abc
EUR
EW
USD

This at least e will not appear in the middle, if we use SortKey:sort-k 1,1n-f

0
0.000
0.0000000000000000000000000000000000000000000000000000
Abc
EUR
EW
USD
0.000000000001
0.1

How can we get g to expel the letters and not be treated as numbers, we can use:lc_all=c sort-k 1,1g-f

Abc
EUR
EW
USD
0
0.000
0.0000000000000000000000000000000000000000000000000000
0.000000000001
0.1

First of all, why now G can be sorted correctly instead of starting with E as 0, this is because Lc_all=c overrides the local environment settings, the sort is sorted by ASCII code, the default should be Lc_all=en_us. UTF, you can also set individual LC separately.

But then the problem comes, and with Lc_all=c, the-F is invalidated because the ASCII lowercase letters are followed by uppercase letters, which creates a conflict.

Therefore, you can perform lc_all=c before sorting.

Lc_all=en_us. UTF Sort-k 1,1g-f can also be sorted as normal.

There is also a need to learn more and to find some other aspects of the problem and the source of the main problems.

Have better information please tell me about sort.

The confusion of Linux sort-g

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.