Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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!

share|improve this question

1 Answer 1

up vote 2 down vote accepted

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).

share|improve this answer
    
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 Jan 21 '12 at 18:11
    
use other means (sed, awk, python, perl etc) to extract the number, put it infront of the filename and then use sort ... –  akira Jan 22 '12 at 7:27
    
Ok, did it with awk. –  Clément Jan 22 '12 at 23:18

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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