Home > Software design >  Sorting with -k
Sorting with -k

Time:11-18

I have this list:

 DI   bpg01001:PGE00  3:1          ------  1           1 (No fault)
 DI   bpg01001:VOL00  2:13         ------  1           1 (Normal)
 DI   dca06001:HPR00  3:12         ------  1           1 (Normal)
 DI   dca06001:HUH00  3:15         ------  1           1 (Normal)
 DI   dca06001:PWS00  3:14         ------  1           1 (Normal)
 DI   dca06001:UOL00  3:13         ------  1           1 (Normal)
 DI   rcf10001:ACO00  2:0          ------  1           1 (Present)
 DI   rcf10001:BDC00  2:4          ------  1           1 (Normal)
 DI   rcf10001:ERR00  2:2          ------  1           1 (Normal)
 DI   rcf10001:ERS00  2:3          ------  1           1 (Normal)
 DO   bpg01001:PGS00  1:4          ------  0           0 (Stop)

My goal is to sort everything from 1:4 to 3:15 but |sort -k3 seems to fail in terms of human readings. Any ideas?

CodePudding user response:

If you sort does not support -V you can do a decorate / sort / undecorate to achieve what you are describing:

awk '{split($3,a,":"); print a[1],a[2],"|" $0}' file | sort -nk1,1 -nk2,2 | sed 's/^[^|]*\|//'

DO   bpg01001:PGS00  1:4          ------  0           0 (Stop)
DI   rcf10001:ACO00  2:0          ------  1           1 (Present)
DI   rcf10001:ERR00  2:2          ------  1           1 (Normal)
DI   rcf10001:ERS00  2:3          ------  1           1 (Normal)
DI   rcf10001:BDC00  2:4          ------  1           1 (Normal)
DI   bpg01001:VOL00  2:13         ------  1           1 (Normal)
DI   bpg01001:PGE00  3:1          ------  1           1 (No fault)
DI   dca06001:HPR00  3:12         ------  1           1 (Normal)
DI   dca06001:UOL00  3:13         ------  1           1 (Normal)
DI   dca06001:PWS00  3:14         ------  1           1 (Normal)
DI   dca06001:HUH00  3:15         ------  1           1 (Normal)

CodePudding user response:

You may try this sort on field 3 with version sort option:

sort -Vk3 file

 DO   bpg01001:PGS00  1:4          ------  0           0 (Stop)
 DI   rcf10001:ACO00  2:0          ------  1           1 (Present)
 DI   rcf10001:ERR00  2:2          ------  1           1 (Normal)
 DI   rcf10001:ERS00  2:3          ------  1           1 (Normal)
 DI   rcf10001:BDC00  2:4          ------  1           1 (Normal)
 DI   bpg01001:VOL00  2:13         ------  1           1 (Normal)
 DI   bpg01001:PGE00  3:1          ------  1           1 (No fault)
 DI   dca06001:HPR00  3:12         ------  1           1 (Normal)
 DI   dca06001:UOL00  3:13         ------  1           1 (Normal)
 DI   dca06001:PWS00  3:14         ------  1           1 (Normal)
 DI   dca06001:HUH00  3:15         ------  1           1 (Normal)

Update: if your sort doesn't support -V then you can try this work around solution:

awk '{print $3 "#" $0}' file | sort -t: -k1n -k2n | cut -d# -f2-

 DO   bpg01001:PGS00  1:4          ------  0           0 (Stop)
 DI   rcf10001:ACO00  2:0          ------  1           1 (Present)
 DI   rcf10001:ERR00  2:2          ------  1           1 (Normal)
 DI   rcf10001:ERS00  2:3          ------  1           1 (Normal)
 DI   rcf10001:BDC00  2:4          ------  1           1 (Normal)
 DI   bpg01001:VOL00  2:13         ------  1           1 (Normal)
 DI   bpg01001:PGE00  3:1          ------  1           1 (No fault)
 DI   dca06001:HPR00  3:12         ------  1           1 (Normal)
 DI   dca06001:UOL00  3:13         ------  1           1 (Normal)
 DI   dca06001:PWS00  3:14         ------  1           1 (Normal)
 DI   dca06001:HUH00  3:15         ------  1           1 (Normal)
  • Related