use strict; use warnings; my $infile; my $outfile; if (!@ARGV or grep /-h|--help/, @ARGV) { print STDERR "Usage: sortmd5 \n"; print STDERR " or sortmd5 \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 () { 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__