Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

Re^4: Extract lines between two patterns

by mnithink (Initiate)
on Jan 22, 2012 at 08:58 UTC ( #949236=note: print w/ replies, xml ) Need Help??


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

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


Comment on Re^4: Extract lines between two patterns
Download Code
Re^5: Extract lines between two patterns
by philipbailey (Chaplain) on Jan 22, 2012 at 09:38 UTC

    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 $_.

Re^5: Extract lines between two patterns
by pvaldes (Chaplain) on Jan 22, 2012 at 21:54 UTC

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

    untested

    #!/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;
      Hi,

      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;
        Thanks to all of you for your help. Based on your guidance i managed to get my code work. I'm posting my code below. #!/usr/bin/perl #use strict; #use warnings; my $fh; my $log; my $line; open($fh,"<","file1") or die "can not open file $fh : $!\n" ; open ($log,">","file2")or die "can not open file $log : $!\n" ; while($line = <$fh>) { if ($line =~ /pattern1/ ) { printf $log $line; while($line = <$fh>) { if($line =~ /pattern2/) { printf $log $line; last; } else { printf $log $line; } } } } close $fh; close $log; Happy to take your advice to make this code more efficient.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://949236]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others lurking in the Monastery: (6)
As of 2014-12-27 03:22 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (176 votes), past polls