A simple state machine will do. You are looking for either the start tag or for the end tag, so process each line in one of those contexts.
#!/usr/bin/perl use strict; use warnings; my $START = "1"; my $END = "2"; my $looking_for_end = 0; my @buffer; while (<DATA>) { if ($looking_for_end) { # Start tag -> Abort and restart if ($_ eq "$START\n") { print(@buffer); @buffer = ($_); } # End tag -> Remove newlines elsif (substr($_, -(length($END)+1)) eq "$END\n") { print(grep { $_ ne "\n" } @buffer); print($_); @buffer = (); $looking_for_end = 0; } # Newline -> Add to buffer (in case we abort) elsif ($_ eq "\n") { push(@buffer, $_); } # Unexpected -> Abort else { print(@buffer); print($_); @buffer = (); $looking_for_end = 0; } } else { # Start tag -> Start buffering and looking for end tag. if ($_ eq "$START\n") { @buffer = ($_); $looking_for_end = 1; } else { print($_); } } } __DATA__ 1 b 1 _____ 2
In reply to Re: Implementing a parsing rule in a huge data file
by ikegami
in thread Implementing a parsing rule in a huge data file
by thezip
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |