open file, delete 4 lines if they meet criteria

  • Thread starter Faith Greenwood
  • Start date
F

Faith Greenwood

OK, I know how to open a file and delete a specific line, but how can
I delete a section of lines? for instance, I have:

here's the contents of my <FILE>:

the red fox
and the brown fox
quickly and quietly
jumped over
the fence
but for some reason
the fence was
too high
I guess they
will have to try and try again
the farmer saw the goat
and the goat was happy

--end of file

I want my ending file to be as follows:

the red fox
and the brown fox
quickly and quietly
jumped over
the fence
the farmer saw the goat
and the goat was happy

The part I will be deleting will obviously be:
but for some reason
the fence was
too high
I guess they
will have to try and try again

and will alway start with "but for some reason" and end with "will
have to try and try again" but the middle contents will change....how
can I do this in perl?


I have this:

while (<FILE>){
next if /but for some reason/;
last if /will have to try and try again/;
print $_;

}

but this gives me:

the red fox
and the brown fox
quickly and quietly
jumped over
the fence
the fence was
too high
I guess they
 
F

Faith Greenwood

You want the .. operator in scalar context.




    perl -ne'print unless /but for/../will have to/'

or the equivalent while loop.

Ben

dude, that is too easy! now that I see it I feel ashamed for even
asking. thx!
-Faith
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,754
Messages
2,569,521
Members
44,995
Latest member
PinupduzSap

Latest Threads

Top