GNU coreutils `sort` behave differently

annahri

I wanted to sort a list of data and I intended to sort it based on its first column which is an IP address.

192.168.1.100
192.168.1.101
192.168.1.110
192.168.1.119
192.168.1.20
192.168.1.30
192.168.1.33
192.168.1.54
192.168.1.64
192.168.1.6
192.168.1.91

On my first machine, I tested sort -n and It worked as I expected

# coreutils, version: 8.31, release: 23

192.168.1.6
192.168.1.20
192.168.1.30
192.168.1.33
192.168.1.54
192.168.1.64
192.168.1.91
192.168.1.100
192.168.1.101
192.168.1.110
192.168.1.119

But on my second machine, it won't sort properly

# coreutils, version:8.4

192.168.1.100
192.168.1.101
192.168.1.110
192.168.1.119
192.168.1.20
192.168.1.30
192.168.1.33
192.168.1.54
192.168.1.6
192.168.1.64
192.168.1.91

Both machines have the same locale en_US.UTF-8


Why is this happening? How can I resolve it?

Inian

Without a proper key position, sort uses the entire line as the key. Since in all the lines, the first three octets remain the same, the entirety of the sorting is based on the numerical positions of the first character in the last octet. Since 1 appears before 2 the octets with 100, 101 appear before the other.

Define the proper key position and use the numerical sort. For e.g. in your case set the delimiter for the input as . and let sort to work its magic on 4th field only. The 4,4 means start at the 4th field delimited by . and stop at the same 4th field.

sort -n -t'.' -k4,4 file

Also you can override any other locale settings defined in your system and directly use the system's default with LC_ALL=C locally to the command. See What does LC_ALL=C do? to understand why

LC_ALL=C sort -n -t'.' -k4,4 file

Thanks to Kamil Maciorowski's comment which highlighted the actual issue.

The first machine seems to be using a locale where locale thousands_sep returns . Probably it's not en_US.UTF-8 (at least not as LC_NUMERIC). The second machine doesn't use . as thousands separator.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Java

Why do Object and var variables behave differently?

From Dev

Why stdout is not equal to 1 in gnu coreutils?

From Dev

JSHint behave differently in Webstorm and Grunt

From Dev

<|> in Parsec - why do these examples behave differently?

From Dev

Perl implementation of the GNU Coreutils

From Dev

Why do while modifiers behave differently with blocks?

From Dev

Why do these generator expressions behave differently?

From Dev

Why does strptime() behave differently on OSX and on Linux?

From Dev

Does import with wildcard behave differently with user package

From Dev

Why does strerror_r behave differently when compiled with gnu90 and c90 standards?

From Dev

Does wildcard characters behave differently?

From Dev

Why does sem (GNU parallel) behave differently with single quotes and double quotes?

From Dev

Github page links behave differently offline and online

From Dev

Why is coreutils sort slower than Python?

From Dev

Ignore blank keys in coreutils sort

From Dev

for attribute in label behave differently in ie

From Dev

Any options to replace GNU coreutils on Linux?

From Dev

Sort by function using bash/coreutils instead of perl

From Dev

rbenv missing GNU coreutils

From Dev

How to make sort consider blanks (GNU coreutils)

From Dev

Is gnu coreutils sort broken?

From Dev

Why GNU find -execdir command behave differently than BSD find?

From Dev

Why does Gnu sort sort differently on my OSX machine and Linux machine?

From Dev

how does the gnu coreutils `date` work?

From Dev

GNU coreutils need suggestions

From Dev

GNU coreutils and GNU Bash

From Dev

Why does sem (GNU parallel) behave differently with single quotes and double quotes?

From Dev

object.ReferenceEquals behave differently

From Dev

Do joins in Hive behave differently?

Related Related

  1. 1

    Why do Object and var variables behave differently?

  2. 2

    Why stdout is not equal to 1 in gnu coreutils?

  3. 3

    JSHint behave differently in Webstorm and Grunt

  4. 4

    <|> in Parsec - why do these examples behave differently?

  5. 5

    Perl implementation of the GNU Coreutils

  6. 6

    Why do while modifiers behave differently with blocks?

  7. 7

    Why do these generator expressions behave differently?

  8. 8

    Why does strptime() behave differently on OSX and on Linux?

  9. 9

    Does import with wildcard behave differently with user package

  10. 10

    Why does strerror_r behave differently when compiled with gnu90 and c90 standards?

  11. 11

    Does wildcard characters behave differently?

  12. 12

    Why does sem (GNU parallel) behave differently with single quotes and double quotes?

  13. 13

    Github page links behave differently offline and online

  14. 14

    Why is coreutils sort slower than Python?

  15. 15

    Ignore blank keys in coreutils sort

  16. 16

    for attribute in label behave differently in ie

  17. 17

    Any options to replace GNU coreutils on Linux?

  18. 18

    Sort by function using bash/coreutils instead of perl

  19. 19

    rbenv missing GNU coreutils

  20. 20

    How to make sort consider blanks (GNU coreutils)

  21. 21

    Is gnu coreutils sort broken?

  22. 22

    Why GNU find -execdir command behave differently than BSD find?

  23. 23

    Why does Gnu sort sort differently on my OSX machine and Linux machine?

  24. 24

    how does the gnu coreutils `date` work?

  25. 25

    GNU coreutils need suggestions

  26. 26

    GNU coreutils and GNU Bash

  27. 27

    Why does sem (GNU parallel) behave differently with single quotes and double quotes?

  28. 28

    object.ReferenceEquals behave differently

  29. 29

    Do joins in Hive behave differently?

HotTag

Archive