Sorting a checksums file by file name

2

I'm trying to sort the output of the md5sum program by file name, numerically (that is, file names all match log-\d+\.txt). I tried

sort -g "-t " -k 2 CHECKSUMS

but it sorts by checksum. Using

sort "-t " -k 2 CHECKSUMS

does sort by filename, but log-12.txt is placed before log-2.txt.

Any clues? Thanks!

Clément

Posted 2012-01-21T07:48:11.003

Reputation: 740

Answers

2

try this:

$> sort -t '-' -k 2 -n CHECKSUMS

it splits the line d41d8cd98f00b204e9800998ecf8427e log-1.txt at the - ... so the number is the firstmost string which allows sort to do the right thing(tm).

akira

Posted 2012-01-21T07:48:11.003

Reputation: 52 754

What if the number of dashes was not the same in every file (e.g. a-1, a-2, a-a-1, b-1, b-a-a-1, b-a-b-1, b-a-b-2, ...)? Is there a way to tell sort to start comparing numerically as soon as it finds a number in both strings being compared? – Clément – 2012-01-21T18:11:07.940

use other means (sed, awk, python, perl etc) to extract the number, put it infront of the filename and then use sort ... – akira – 2012-01-22T07:27:07.740

Ok, did it with awk. – Clément – 2012-01-22T23:18:10.577