output:use strict; use warnings; process_file( \*DATA ); exit 0; sub process_file { my ($fh) = @_; while ( my $line = <$fh> ) { print $line; if ( $line =~ /section start marker/ ) { handle_section($fh); } } } sub handle_section { my ($fh) = @_; my ( @entries, $line ); while ( $line = <$fh> ) { last unless $line =~ /^entry/; push @entries, $line; } print map { $_->[1] } sort { $a->[0] cmp $b->[0] } map { [ get_cmp_key(), $_ ] } @entries; print $line if defined $line; } sub get_cmp_key { m{ \( (\d+) \s+ (.+) }x or die "Can't match '$_'!"; # inspired by johngg :) return pack 'Na*', $1, $2; } __DATA__ [section start marker (shift-opt-5)] [some line of text] 1 and no entries 0 none at all! [section start marker (shift-opt-5)] [some line of text] entry \(7 data\) entry \(5 data\) entry \(6 data\) [section start marker (shift-opt-5)] [some line of text] entry \(001 data\) entry \(1 data\) entry \(01 data\) ^ those are equivalent as numbers text C text B text A [section start marker (shift-opt-5)] [some line of text] entry \(001 dataC\) entry \(1 dataB\) entry \(01 dataA\)
[section start marker (shift-opt-5)] [some line of text] 1 and no entries 0 none at all! [section start marker (shift-opt-5)] [some line of text] entry \(5 data\) entry \(6 data\) entry \(7 data\) [section start marker (shift-opt-5)] [some line of text] entry \(001 data\) entry \(1 data\) entry \(01 data\) ^ those are equivalent as numbers text C text B text A [section start marker (shift-opt-5)] [some line of text] entry \(01 dataA\) entry \(1 dataB\) entry \(001 dataC\)
In reply to Re: Use Perl's Sort to only sort certain lines in a file?
by Anonymous Monk
in thread Use Perl's Sort to only sort certain lines in a file?
by grahambuck
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |