in reply to If line is followed by line, delete both lines
Hello Ganesh Bharadwaj1,
If your input lines are always paired, then Marshall’s approach is probably the best. Otherwise, your logic will need to be a bit more complicated. For example:
use strict; use warnings; my $previous_line; my $exclude = 0; while (my $current_line = <DATA>) { if (defined $previous_line) { if ($previous_line =~ /cat/ && $current_line =~ /dog/) { $exclude = 1; } else { emit_line($previous_line) unless $exclude; $exclude = 0; } } $previous_line = $current_line; } emit_line($previous_line) unless $exclude; sub emit_line { my ($line) = @_; print $line if defined $line; } __DATA__ dog hippopotamus cat dog dog cat goat
Output:
17:11 >perl 1695_SoPW.pl dog hippopotamus dog cat goat 17:11 >
Hope that helps,
| Athanasius <°(((>< contra mundum | Iustus alius egestas vitae, eros Piratica, |
|
|---|