http://www.perlmonks.org?node_id=711734

Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

I have a template as follows:
**Temp** fdvfd fgd dfgd dfgd **End Temp ** this is test dffdgd this is test dffdgd
I want to be able to delete the lines from **Temp** to **End Temp**. is there a way to do it? Thanks

Replies are listed 'Best First'.
Re: How to delete contents between two strings.
by toolic (Bishop) on Sep 16, 2008 at 16:40 UTC
    Use the Range Operators:
    use strict; use warnings; while (<DATA>) { print unless (/\*\*Temp/ .. /\*\*End/); } __DATA__ **Temp** fdvfd fgd dfgd dfgd **End Temp ** this is test dffdgd this is test dffdgd

    prints:

    this is test dffdgd this is test dffdgd

    Update: Please correct the typo in your title: s/conetents/contents/ Nevermind, Anonymous Monk

      Uuii, the Range Operator in scalar/boolean mode. I just learned this year about it. Very nice solution, toolic.

      In case the Anonymous Monk, or someone else with a similar homework ;-), needs the ** lines included, the feature that '..' in scalar mode returns an increment for every true evaluation and adds 'E0' on the last, can be used:

      #!/usr/bin/perl use strict; use warnings; while (<DATA>) { my $i = (/\*\*Temp/ .. /\*\*End/); print unless ($i && $i > 1 && substr($i, -2) ne 'E0'); } __DATA__ first second **Temp** fdvfd fgd dfgd dfgd **End Temp ** this is test dffdgd this is test dffdgd

      prints:

      first second **Temp** **End Temp ** this is test dffdgd this is test dffdgd
Re: How to delete conetents between two strings.
by mr_mischief (Monsignor) on Sep 16, 2008 at 16:53 UTC
    The easiest conceptually may be to use a flag variable to tell when you're between the markers. Your code can then print a copy only when the lines are not between the flags. This will process line by line, so it should use relatively little memory regardless of the length of the file and the length of the excluded sections.

    This smells a bit like homework but could be very usful to any random sysadmin, too. I'm going to put the actual code in a spoiler tag and hope you choose honorably based on your situation.

    Another way is to slurp the data and use a multi-line regex replacement. That won't take too much memory if there aren't too many lines (for some values of "too much" and "too many"). Implementation is left as an exercise.

    There are likely many more exotic ways to handle this as well, but one or the other of the ones already mentioned should work for you.

Re: How to delete conetents between two strings.
by Erez (Priest) on Sep 16, 2008 at 16:44 UTC

    Not only there is *a* way to do it, but there is more than one way to do it!
    Seriously, here's what you should do:
    A: read the file.
    B: store only what you need based on the criteria you have been given
    C: return what you stored
    D: don't expect others to do your work.

    Stop saying 'script'. Stop saying 'line-noise'.
    We have nothing to lose but our metaphors.

Re: How to delete conetents between two strings.
by Narveson (Chaplain) on Sep 16, 2008 at 16:44 UTC

    Slurp your stuff into a multi-line string, call it $stuff.

    my $TEMP_PATTERN = qr{ \*\*Temp\*\* .* \*\*End \s+ Temp \s* \*\* }msx; $stuff =~ s/$TEMP_PATTERN//;
      Thanks a lot. It worked.