Sorting lines in a file by a numeric value burried inside

POSIXly, you can do:

sort -t '(' -k2n < file

That is set the field separator to ( and sort on the second field (or rather the portion of the line starting with the second field) numerically.

Alternatively, you could keep the default field separator (the transition from a non-blank to a blank) where the 5th field would be like " (12" and use:

sort -k5.3n < file

(that is, sort numerically on the portion of the line starting with the 3rd character of the 5th field).

For ties, the last-resort sorting order comes into play, and that's lexical comparison of the full line (which conveniently here will give you a chronological order).

If you wanted to sort ties on drive name instead, you could use:

sort -t '(' -k2n -k1.21

(second key being the portion of line starting with the 21st character, lexical comparison)


Use sort -V if that option is available

-V, --version-sort

natural sort of (version) numbers within text

$ <cmd> | sort -k5,5V
2017-10-25_09:30:22/sdq.log:Data LOST: 2.00 KB (4 sectors)
2017-10-26_09:17:59/sde.log:Data LOST: 2.00 KB (4 sectors)
2017-10-26_09:17:59/sdp.log:Data LOST: 2.00 KB (4 sectors)
2017-10-25_14:37:03/sdc.log:Data LOST: 3.00 KB (6 sectors)
2017-10-26_09:17:59/sdd.log:Data LOST: 3.00 KB (6 sectors)
2017-10-26_09:17:59/sdf.log:Data LOST: 3.00 KB (6 sectors)
2017-10-26_09:17:59/sdo.log:Data LOST: 3.00 KB (6 sectors)
2017-10-25_09:30:22/sdf.log:Data LOST: 4.00 KB (8 sectors)
2017-10-25_09:30:22/sdo.log:Data LOST: 4.00 KB (8 sectors)
2017-10-25_09:30:22/sdp.log:Data LOST: 4.00 KB (8 sectors)
2017-10-25_09:30:22/sdi.log:Data LOST: 5.00 KB (10 sectors)
2017-10-25_09:30:22/sdn.log:Data LOST: 5.00 KB (10 sectors)
2017-10-25_14:37:03/sdb.log:Data LOST: 5.00 KB (10 sectors)
2017-10-26_09:17:59/sds.log:Data LOST: 5.00 KB (10 sectors)
2017-10-26_09:17:59/sdg.log:Data LOST: 6.00 KB (12 sectors)
2017-10-26_09:17:59/sdi.log:Data LOST: 6.00 KB (12 sectors)
2017-10-26_09:17:59/sdl.log:Data LOST: 6.00 KB (12 sectors)
2017-10-25_09:30:22/sdj.log:Data LOST: 2.35 MB (4822 sectors)
2017-10-26_09:17:59/sdr.log:Data LOST: 65.29 MB (133712 sectors)
2017-10-26_09:17:59/sdq.log:Data LOST: 414.60 MB (849106 sectors)
2017-10-26_09:17:59/sdh.log:Data LOST: 611.29 MB (1251918 sectors)

I've found it.

sort -n -t '(' -k2V

-n tells sort to read numeric values in the strings

-t '(' tells it to use ( character as field delimiter. Becasue the word sectors) is always the same later on it won't affect the sorting order.

-k2V defines a custom key, using the second column - text after first ( character - for sorting.

Tags:

Sort