Perl – Rechercher une expression et garder plusieurs lignes

Imaginez le texte :

2008-01-02: first entry
2008-02-03: second entry on two lines
    here is the additional line
2008-03-04: third entry
   has
   three
   extra lines
2008-04-05: fourth entry has just one on line again

Question :

Comment faire pour avoir le groupe de lignes qui contient un mot, par exemple « three»  ?

Solution :

my @stuff;
while (<IN>) {
    if (/^\s/) { $stuff[-1] .= $_; }
    else { push @stuff, $_;  }
}
print grep { /three/ } @stuff;

Donne :

2008-03-04: third entry
   has
   three
   extra lines

Source


  Partager sur Wikio

1 Response to “Perl – Rechercher une expression et garder plusieurs lignes”


  1. 1 William Ward

    I think this is the first time someone translated one of my Perl Tips into French. Very cool.

Leave a Reply