in reply to Re: Parsing a text file in Perl.
in thread Parsing a text file in Perl.

I wrote the below code, but its not matching:
open (FILE, '<', '123.log') or die "Could not open 123.log: $!"; while (<FILE>) { #print $_ if (/^[==========]/ .. /^tests.../); if (/^[==========]/ .. /^tests.../){ print "Line Found:".$_."\n"; } } close (FILE) or die "Could not close 123.log: $!";

Replies are listed 'Best First'.
Re^3: Parsing a text file in Perl.
by Athanasius (Archbishop) on May 26, 2014 at 11:11 UTC

    If you want to match [==========] in a regex, you need to escape the square brackets — otherwise they form a character class.

    However, this approach cannot work: you are reading the data file line-by-line, which to Perl means from one newline (\n) to the next, but a typical line of input looks like this:

    < 0x00070: 2d 64 6f 77 6e 0d 0a 5b 3d 3d 3d 3d 3d 3d 3d 3d -down.. +[========

    As can be seen, there are no lines which match [==========]. Likewise, the special regex character ^ matches at the beginning of a line, and neither [==========] nor tests appears at the beginning of a line.

    You will need a different strategy, along the lines outlined by hippo below.

    Hope that helps,

    Athanasius <°(((><contra mundum Iustus alius egestas vitae, eros Piratica,

Re^3: Parsing a text file in Perl.
by ramki067 (Acolyte) on May 26, 2014 at 11:07 UTC
    I tried one more shot with the below but no luck.
    open (FILE, '<', '123.log') or die "Could not 123.log: $!"; my $i=0; while (<FILE>) { #print $_ if (/^[==========]/ .. /^tests./); if (/^[0-9] tests from [0-9] test cases ran/ .. /^[0-9] tests\./){ print "$i.Match Found:".$_."\n"; $i++; } }
    Any help?