Hi,
the following script is sorting MD5 checksum files after filename. If only one filename is given the sorted result is written back to the input file.

Also works with SHA1 checksum files or similar.

use strict; use warnings; my $infile; my $outfile; if (!@ARGV or grep /-h|--help/, @ARGV) { print STDERR "Usage: sortmd5 <md5sum file>\n"; print STDERR " or sortmd5 <md5sum infile> <md5sum outfile>\n\n" +; print STDERR "First usage changes md5sum file inplace. The string +'-' can ", "be given to specify STDIN or STDOUT.\n"; exit 1; } if (@ARGV) { $infile = $ARGV[0] unless $ARGV[0] eq '-'; $outfile = (@ARGV > 1) ? $ARGV[1] : $ARGV[0]; undef $outfile if $outfile eq '-'; } open (STDIN, '<', $infile) if defined $infile; my %md5; my $badline = 0; while (<STDIN>) { if (/^([a-f0-9]{32,} [* ])(.*)\z/si) { $md5{$2} = $1; } else { $_ =~ s/\r?\n?\z//; print STDERR "Found bad line: $_\n"; $badline++ } } close (STDIN); rename ($infile, $infile . '.bak') if defined $infile and $badline; { no warnings; open (STDOUT, '>', $outfile) if defined $outfile; } foreach my $file (sort keys %md5) { print "$md5{$file}$file"; } exit 0; __END__

In reply to sortmd5 script by mscharrer

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.