Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical

Re: Extract lines between two patterns

by GrandFather (Sage)
on Jan 22, 2012 at 07:04 UTC ( #949230=note: print w/replies, xml ) Need Help??

in reply to Extract lines between two patterns

What have you tried and how did it fail? We can help you much better if we can see your code, and better still if you can provide a self contained sample script along with your description. See I know what I mean. Why don't you? for some guidance.

True laziness is hard work

Replies are listed 'Best First'.
Re^2: Extract lines between two patterns
by mnithink (Initiate) on Jan 22, 2012 at 07:21 UTC
    Thanks for the reply First I experimented with this below code on a single file.
    perl -ne 'print if /Pattern1/ .. /Pattern2/' file1
    This worked for me. But my actual intent is to do this in a loop on multiple files. My code looks as below
    #!/usr/bin/perl use strict; use warnings; our $fh; our $log; open($fh,">","file1"); open ($log,">","file2") or die "can not open file $log : $!\n" ; while($fh){ if (/Pattern1 .. /Pattern2/) {printf $log;} }
    when i execute this code, i'm getting below message
    "Use of uninitialized value in pattern match (m//) at line 11. +"

      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.

        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; } }

Log In?

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

How do I use this? | Other CB clients
Other Users?
Others studying the Monastery: (4)
As of 2018-05-21 03:48 GMT
Find Nodes?
    Voting Booth?