in reply to Re^5: Extract lines between two patterns
in thread Extract lines between two patterns
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;
|
---|
Replies are listed 'Best First'. | |
---|---|
Re^7: Extract lines between two patterns
by mnithink (Initiate) on Jan 24, 2012 at 03:27 UTC | |
by Anonymous Monk on Oct 11, 2012 at 17:02 UTC | |
by Anonymous Monk on Oct 12, 2012 at 07:37 UTC | |
by Anonymous Monk on Oct 12, 2012 at 13:38 UTC |
In Section
Seekers of Perl Wisdom