Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .