Beefy Boxes and Bandwidth Generously Provided by pair Networks vroom
Just another Perl shrine
 
PerlMonks  

How to delete conetents between two strings.

by Anonymous Monk
on Sep 16, 2008 at 16:29 UTC ( #711734=perlquestion: print w/ replies, xml ) Need Help??
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

Comment on How to delete conetents between two strings.
Download Code
Re: How to delete contents between two strings.
by toolic (Chancellor) 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 Erez (Curate) 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.
Re: How to delete conetents between two strings.
by mr_mischief (Prior) 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.

    my $remove = 0; while ( <> ) { if ( $remove ) { # don't print $remove = 0 if /^\*\*End Temp \*\*/; } elsif ( /^\*\*Temp\*\*/ ) { $remove = 1; } else { print $_; } }

    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.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://711734]
Approved by toolic
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others taking refuge in the Monastery: (13)
As of 2014-04-25 08:32 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    April first is:







    Results (585 votes), past polls