1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53
| #!/usr/bin/perl
use strict; use warnings;
my $basedir = 'C:/Documents and Settings/farid/Bureau/9cegetel';
open my($src),'<', "$basedir/test_out.txt"
or die "E/S : $!\n";
open my($out), '>>', "$basedir/resultat.txt"
or die "E/S : $!\n";
# liste des commandes
my @commands = qw(
SRFIL
ESMIN
GLRIL
ALAIL
ANOIL
NSSIN
DMPIL
);
# construction de la regex pour reconnaître l'une de ces commandes
# en début de ligne (éventuellement après quelques espaces/tabulations)
# on capture la commande utilisée
# aspect final de la regex : m/^\s*(SRFIL|ESMIN|...|DMPIL)/
my $rx_string = join '|', map { quotemeta $_ } @commands;
my $rx_commands = qr/^\s*($rx_string)/;
# variable d'état pour savoir si l'on se trouve dans un paragraphe
# qu'on veut imprimer ($in == 2), après une commande ($in == 1),
# ou en dehors de tout cela ($in == 0)
my $in = 0;
while(<$src>){
if( not $in and my ($cmd) = (m/$rx_commands/) ) {
print $out "$cmd:\n\n";
$in = 1;
}
elsif( $in == 1 and m/^\s*TRAITEMENT TDRHM0 ACC/ ) {
$in = 2;
}
elsif( $in == 2 and m/^\s*TRAITEMENT TDRHM0 EXC/ ) {
$in = 0;
print $out "\n\n";
}
elsif( $in == 2 ) {
print $out $_;
}
}
close $src;
close $out; |