On Fri, 2008-07-04 at 21:32 -0500, Brian Funk wrote:
> I need a starting point to learn to accomplish the following action:
> 

> In short, I want to see "Foo" in the file and delete the next two lines 
> following. This pattern can repeat hundreds of times within the file and 
> spread across multiple files.
> 
> I'd rather learn to write something in preference to using a canned module.

Here is a quick solution requiring one flag and a counter. It skips
printing the two lines after a "Foo" line. The skip_count flag is reset
each time a "Foo" line appears in the input.  Then the next two lines
are skipped.  Once the two lines are skipped, subsequent lines are
printed until the next "Foo" line is found.

There are ways to generalize and parameterize this algorithm.  I will
leave that as an exercise.

my $found_foo = 0;
my $skip_count = 0;
while (<>) {
        chomp;

        if (/^Foo$/) {
                $found_foo = 1;
                print "$_\n";
                next;
        }

        if ($found_foo) {
                $skip_count++;
                next if $skip_count <= 2;
        }

        $found_foo = 0;
        $skip_count = 0;
        print "$_\n";
}

-- 
Smoot Carl-Mitchell
System/Network Architect
[EMAIL PROTECTED]
+1 480 922 7313
cell: +1 602 421 9005

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/


Reply via email to