open file, delete 4 lines if they meet criteria

Discussion in 'Perl Misc' started by Faith Greenwood, Aug 1, 2009.

  1. 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
     
    Faith Greenwood, Aug 1, 2009
    #1
    1. Advertising

  2. On Aug 1, 12:56 pm, Ben Morrow <> wrote:
    > Quoth 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:

    >
    > You want the .. operator in scalar context.
    >
    > <snip>
    >
    > > 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?

    >
    >     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
     
    Faith Greenwood, Aug 1, 2009
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. ÀÏÓà
    Replies:
    0
    Views:
    365
    ÀÏÓà
    May 4, 2005
  2. HeroOfSpielburg
    Replies:
    1
    Views:
    394
    Alf P. Steinbach
    Aug 6, 2003
  3. Steve Holden
    Replies:
    7
    Views:
    470
    Christos TZOTZIOY Georgiou
    Mar 30, 2005
  4. Murali
    Replies:
    2
    Views:
    577
    Jerry Coffin
    Mar 9, 2006
  5. Cory Mottice
    Replies:
    2
    Views:
    141
    Dave Angel
    Sep 12, 2013
Loading...

Share This Page