0

I have a file like so:

1.1
3.2
1.2
1.10

I would like to sort the file so that it looks like so:

1.1
1.2
1.10
3.2

In other words, 1.10 is bigger than 1.2

I tried:

sort -nk 1,1 file

But I keep getting this, which is not what I want

1.1
1.10
1.2
3.2

Thanks

Cyrus
  • 84,225
  • 14
  • 89
  • 153
mf94
  • 439
  • 4
  • 19
  • 1
    On Linux (or, more accurately, with GNU `sort`), there's an option `-g` or `--general-numeric-sort`. I've not played with it. – Jonathan Leffler Sep 15 '17 at 16:27
  • 1
    On second thoughts, you're not interested in treating the value as a number, because numerically, 1.10 is the same as 1.1 and is smaller than 1.2. So, the `-g` option is no help. The `-V` option treats components as components of a version; that works if you have it available. – Jonathan Leffler Sep 15 '17 at 16:37

2 Answers2

2

With GNU sort:

sort -t "." -n -k1,1 -k2,2 file

Output:

1.1
1.2
1.10
3.2
Cyrus
  • 84,225
  • 14
  • 89
  • 153
  • This works correctly because of the requirement that 10 sorts after 2, which is not a numeric sort. It could get tricky in more general contexts where the numbers have more varied numbers of digits (12.1, 2.345) and, in particular, are embedded in fields between other delimiters: `a 12.1 b` etc. I'm not sure there's an easy fix for the latter, other than `-V`. – Jonathan Leffler Sep 15 '17 at 16:36
1

You may use the -V option.

sort -V numbers

However this option is only in GNU Coreutils and could be absent from other implementation.

See https://stackoverflow.com/a/35386002/1107536

Xavier Delamotte
  • 3,519
  • 19
  • 30