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;
In reply to Re^6: Extract lines between two patterns
by kielstirling
in thread Extract lines between two patterns
by mnithink
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |