Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight

Re^3: Extract lines between two patterns

by philipbailey (Chaplain)
on Jan 22, 2012 at 08:39 UTC ( #949235=note: print w/replies, xml ) Need Help??

in reply to Re^2: Extract lines between two patterns
in thread Extract lines between two patterns

There is nothing much wrong with the code you have posted (with the provisos mentioned below) but the problem appears to be in the pattern(s) in your if statement. The error message indicates that you are using some undefined variable in one or other of the pattern matches in that statement. However, you don't have any variables within the patterns shown--perhaps you should post the actual code?

A couple of other comments: please put your code in <code> tags--this would make it much more readable. You would probably be better to use lexical (my) rather than package (our) variables in your use case. Finally, you are missing a terminating "/" in the first pattern match, presumably because you have retyped a shortened version of your code. There is nothing wrong with showing PerlMonks a shortened version of your code--in fact it is encouraged--but it needs to be runnable, and demonstrate the bug.

  • Comment on Re^3: Extract lines between two patterns

Replies are listed 'Best First'.
Re^4: Extract lines between two patterns
by mnithink (Initiate) on Jan 22, 2012 at 08:58 UTC
    Thanks for your commnets. I'll try to improve my coding skills. Please find my actual code below.
    #!/usr/bin/perl use strict; use warnings; our $fh; our $log; open($fh,">","block.power"); open ($log,">","temp.log") or die "can not open file $log : $!\n" ; while($fh){ if (/Total Design Power Summary \(Worst/ .. /\|Total/) { printf $log; } }

      Please don't ignore requests to use <code> tags. You can edit your posts and add them. Having posted runnable code, the issues become more obvious: you open file "block.power" for writing, not reading--so use "<", not ">" (and you don't test whether the open succeeds). Your while loop loops infinitely over "$fh", which is always true; you should say while (<$fh>) {, which actually reads from the filehandle. The undefined variable in the pattern match is in fact $_.

      I want to extract lines between Pattern1 and Pattern2 and write it into file2


      #!/usr/bin/perl use strict; use warnings; our $fh; our $log; open($fh,"<","block.power"); open ($log,">","temp.log") or die "can not open file $log : $!\n" ; while($fh){ if (/Pattern1>(.*?)<Pattern2/) { print $log $1; } } close $fh; close $log;

        What you are doing will fail. When you slurp in a file using a while loop it is read in a line at a time.

        You also need to add < an > around your file handle. In your example you are testing if $fh is defined. Your code will never break out of the loop. In the following code I open the input file, loop over it looking for the start pattern and then end pattern. I then use a var to toggle true/false Not the best way to do this however it works.

        -Kiel R Stirling
        #!/usr/bin/perl -w use strict; open IN, "/tmp/p.txt" or die $!; open OUT, ">/tmp/temp.log" or die "can not open file log : $!\n" ; my $start = 0; while(<IN>){ if (/<Pattern1/) { $start = 1; next; } elsif (/<Pattern2/) { $start = 0; next; } print OUT if $start; } close IN; close OUT;

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://949235]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others taking refuge in the Monastery: (8)
As of 2018-04-25 07:53 GMT
Find Nodes?
    Voting Booth?