Skip to content
Tags

Perl – Rechercher une expression et garder plusieurs lignes

by Cyril on octobre 23rd, 2008

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

Autres articles susceptibles de vous intéresser :

From → System

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

Leave a Reply

Note: XHTML is allowed. Your email address will never be published.

Subscribe to this comment feed via RSS