11

I would like to sort a file :

   D104HN-D104HA      8.320      4.521   1.69e+05 -- 
   D104HN-D104HB*      8.320      2.823   2.93e+05 -- 
   A90HB#-A90HA      1.655      4.207   7.12e+05 -- 
   A90HB#-F91HA      1.653      4.411   8.59e+04 -- 
   A114HB#-A114HA      1.253      4.098   7.67e+05 -- 
   A114HB#-R111HA      1.251      3.929   1.76e+05 -- 
   A114HB#-W110HA      1.253      4.451   3.68e+04 -- 
   F91HE*-F91HZ      7.237      7.122   7.85e+05 -- 
     F91HE*-K92HA      7.242      3.910   4.88e+04 -- 

and get this

   A90HB#-A90HA      1.655      4.207   7.12e+05 -- 
   A90HB#-F91HA      1.653      4.411   8.59e+04 -- 
   F91HE*-F91HZ      7.237      7.122   7.85e+05 -- 
   F91HE*-K92HA      7.242      3.910   4.88e+04 -- 
   D104HN-D104HA      8.320      4.521   1.69e+05 -- 
   D104HN-D104HB*      8.320      2.823   2.93e+05 -- 
   A114HB#-A114HA      1.253      4.098   7.67e+05 -- 
   A114HB#-R111HA      1.251      3.929   1.76e+05 -- 
   A114HB#-W110HA      1.253      4.451   3.68e+04 -- 

That is, sort numerically on the second and following characters of the first sequence of non-blanks on the line.

  • apparently the editor didn't keep the format I used for the file.... – edith Jan 22 '16 at 13:12
  • Have you tried the sort command? – Eric Renouf Jan 22 '16 at 13:15
  • yes, but apparently I don't know how tu use it properly.... I tried :sort -b +0.1n -1 but also sort -n -k1,1 -k1,4 (and more others) but none of those worked – edith Jan 22 '16 at 13:21
  • 1
    You want to sort according to the number starting from the second character? – pfnuesel Jan 22 '16 at 13:23
  • exactly. the probleme is that in the file, there are different numbers of blank before the first field – edith Jan 22 '16 at 13:24
19
sort -k 1.2bn < file

Sorts numerically on a key starting with the 2nd character of the 1st field ignoring leading blanks (and ending at the end of the line, but that doesn't matter for a numerical sort which only considers the initial sequence of decimal digits).

Note that if there's a tie, like in between these two lines:

    F91HE*-K92HA      7.242      3.910   4.88e+04 --
  F91HE*-F91HZ      7.237      7.122   7.85e+05 --

Then, the order will be based on the last-resort sort that compares the two lines fully as strings.

With some locales, blanks would be ignored in first instance for comparing strings in which case the F91 one would come before K92 (as F sorts before K). With others like in the C locale, blanks would not be ignored and the K92 would then come before the F91 one (space sorts before F).

For a last-resort sort that would ignore leading blanks regardless of the locale, you could do:

sort -k 1.2bn -k 1b < file

That 1b one would sort (upon ties on the first key) lexically on the section of the line that starts with the first field ignoring leading blanks.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.