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__
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: sortmd5 script
by talexb (Chancellor) on Aug 13, 2008 at 15:22 UTC | |
by zentara (Cardinal) on Aug 13, 2008 at 15:57 UTC | |
by blazar (Canon) on Aug 14, 2008 at 16:53 UTC | |
by parv (Parson) on Aug 15, 2008 at 00:47 UTC | |
by massa (Hermit) on Aug 15, 2008 at 01:58 UTC | |
by mscharrer (Hermit) on Aug 28, 2008 at 12:57 UTC | |
by talexb (Chancellor) on Aug 28, 2008 at 14:54 UTC |