On Nov 7, 2013, at 2:28 AM, David Precious wrote: > On Wed, 6 Nov 2013 23:51:04 +0000 > "Wang, Li" <li.w...@ttu.edu> wrote: > >> Dear Perl Users >> >> I have hundreds of input files, named as geneName_paml_formated.mlc >> In each file, there are some contents similar as follows: >> >> w (dN/dS) for branches: 0.00010 1.07967 145.81217 0.00010 >> dN & dS for each branch >> branch t N S dN/dS dN dS N*dN S*dS >> 4..5 0.000 1103.3 327.7 0.0001 0.0000 0.0000 0.0 0.0 >> 5..1 0.043 1103.3 327.7 1.0797 0.0144 0.0134 15.9 4.4 > [...] > >> I want to extract the line start with "5..1" to be extracted and [...] > [...] >> It turns out that it didnot work, and seems that the script cannot >> find my expression pattern $line=~m/^5\.\.1/ > > You're looking for a line starting with 5, but it doesn't - it starts > with some whitespace, then 5. > > /^\s*5\.\.1/ should match. > > (\s* allowing zero or more whitespace characters before the 5)
Except the line $line=~s/^\s+//g; that precedes the match should remove the leading whitespace. (Note that /g modifier is superfluous in this case, as there can be only one match anchored to the beginning of the string.) However, your test that includes the leading whitespace is more efficient, since by using that test, you needn't remove leading whitespace from every line. So I would reverse the two initial lines from the while loop (and use the extended regular expression syntax for readability): while( my $line = <IN> ) { if( $line =~ m{ \A \s* 5\.\.1 }x ) { $line =~ s{ \A \s+ }{}x; ... I also note that the original program prints the message "no such line found" if a line doesn't match. That means that message will be printed for every line that doesn't match the pattern. I doubt that is what is wanted. Maybe the program should define a flag variable before the loop, set it to true if a match is found, and test the variable after the loop to see if ANY line matched before printing out the "no such line found" message. -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/