0

I have a file which contains the following

blah
blah blah
Zebra
Blah blah blah
Blah
Bleh
Dog
Blag 
Noblah
Someblah

I want to remove the line segment from Zebra and Dog. Remove only those where Zebra occurs first and Dog occurs later.

How to do this in a perl script ?

Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563

2 Answers2

4

Like How do I remove a specific area of element from an array, this is a use case for the flip-flop operator.

perl -ne 'print unless /Zebra/ .. /Dog/' < input-file
mob
  • 117,087
  • 18
  • 149
  • 283
3
perl -ne'$r ||= /^Zebra$/; print if !$r; $r &&= !/^Dog$/;'

or

perl -ne'print if !( /^Zebra$/ .. /^Dog$/ );'

These assume every Zebra and Dogs are paired, and that they can't be nested.

See Specifying file to process to Perl one-liner.

ikegami
  • 367,544
  • 15
  • 269
  • 518