2

I am trying to sort the below numbers but always its getting sorted 10 before 9.

$ cat disks
1I:1:1
1I:1:3
1I:1:2
1I:1:4
2I:1:5
2I:1:6
2I:1:7
2I:1:8
3I:1:9
3I:1:10
3I:1:12
3I:1:11

After sorting using sort -n its coming as below.

$ sort -n -o sorted /tmp/disks
$ cat sorted
1I:1:1
1I:1:2
1I:1:3
1I:1:4
2I:1:5
2I:1:5
2I:1:7
2I:1:8
3I:1:10
3I:1:11
3I:1:12
3I:1:9    ---> wrong sort

Please let me know which sort option can do this properly ?

oguz ismail
  • 1
  • 16
  • 47
  • 69
SARATH CHANDRAN
  • 307
  • 1
  • 6
  • 16
  • 1
    Possible duplicate of [Sort by third column leaving first and second column intact in Linux?](https://stackoverflow.com/questions/11006431/sort-by-third-column-leaving-first-and-second-column-intact-in-linux) – oguz ismail Mar 29 '20 at 07:06

3 Answers3

2

Try this

sort -n -t: -k3 file.txt -o out.txt
Jetchisel
  • 7,493
  • 2
  • 19
  • 18
2

You can use version sort if your sort supports this option:

sort -V -o sorted /tmp/disks
Freddy
  • 4,548
  • 1
  • 7
  • 17
2

The additional --stable option preserves ordering should you happen to have duplicates in column 3.

$ sort -t: -k3n --stable -o sorted /tmp/disks
$ cat sorted
1I:1:1
1I:1:2
1I:1:3
1I:1:4
2I:1:5
2I:1:6
2I:1:7
2I:1:8
3I:1:9
3I:1:10
3I:1:11
3I:1:12
$ 
rtx13
  • 2,580
  • 1
  • 6
  • 22