in reply to Looking for elegance
If the file is not too large to read into memory you can slurp the whole thing and then split into "records" at points preceded by a line terminator and followed by a digit. You can then sort using a Scwhartzian Transform.
$ perl -Mstrict -Mwarnings -E ' open my $inFH, q{<}, \ <<EOD or die $!; 2 [...] [line1] [line2] [line3] 3 [...] [line1] 1 [...] [line1] [line2] 2 [...] [line1] [line2] EOD my $input = do { local $/; <$inFH>; }; close $inFH or die $!; print for map { $_->[ 0 ] } sort { $a->[ 1 ] <=> $b->[ 1 ] } map { [ $_, m{\A(\d+)} ] } split m{(?<=\n)(?=\d)}, $input;' 1 [...] [line1] [line2] 2 [...] [line1] [line2] [line3] 2 [...] [line1] [line2] 3 [...] [line1] $
I hope this is helpful.
Cheers,
JohnGG
|
|---|