use strict; my %hash; my $entry; my $line; my $key; open(INFILE, "data") || die; while ($line = ) { chomp($line); if ($line) { # in an entry if (!$entry) { # this is the first line $entry .= $line . "\n"; ($key = $line) =~ s/\D//g; } else { $entry .= $line . "\n"; } } else { # left the entry $hash{$key} = $entry; $entry = ""; } } close(INFILE); open(OUTFILE, ">datax") || die; foreach $key (sort(keys(%hash))) { print OUTFILE $hash{$key} . "\n"; } close(OUTFILE);