require 5.000; use warnings; use strict; use POSIX; my %tags = (); my $input = $ARGV[0]; my $output = $ARGV[1]; open (FILE, "< $input") or die "cannot open $input: $!\n"; while (my $tag = ) { $tag =~ m//; if ($tag eq "\n") { # BLANK LINE } else { $tag =~ m//; $tags{sprintf("%04d%6s",$1 || 9999,$2)} = $tag; } } close FILE; open (NEWFILE, "> $output") or die "cannot open $output: $!\n"; foreach my $id ( map { $_ = $_->[2] } sort { $a->[1] cmp $b->[1] || $a->[0] <=> $b->[0] } map { /(\d+)([a-z]*)/; $_ = [$1, $2, $_] } keys %tags ) { print NEWFILE $tags{$id}; print $tags{$id}; } close NEWFILE;