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

Hi all, New perl user. I have a text file with some paragraphs in it. I am trying to delete a repetitive piece of text. The file looks something like this:

keep keep keep keep keep i want to start deleting here blah blah blah blah blah blah blah up to the newline.

keep keep keep keep keep i want to start deleting here blah blah blah blah blah blah blah up to the newline.

keep keep keep keep keep i want to start deleting here blah blah blah blah blah blah blah up to the newline.

the text where i want to start deleting is always the same phrase and i want to delete the paragraph from that point until the newline in that paragraph. It isn't in every paragraph. I need to do it for a few paragraphs from the file.

I would appreciate any help. Thanks.

----------------------------UPDATE---------------------------------------
Thanks all,

That was incredibly helpful. I wound up using a stream variable for the file and instead of looping through just used a global replace, so my code looked like this:

$stream =~ s/$deleteFromPhrase.*//g;

It works perfectly :)

Loving Perl!

Thanks!

Replies are listed 'Best First'.
Re: Trying to delete text from a text file
by toolic (Bishop) on Jul 17, 2010 at 01:11 UTC
    The following should get you started. It uses Perl's substitution operator to delete everything to the end of the line:
    use strict; use warnings; while (<DATA>) { s/(i want to start deleting here).*//; print; } __DATA__ keep keep keep keep keep i want to start deleting here blah blah blah +blah blah blah blah up to the newline. keep keep keep keep keep i want to start deleting here blah blah blah +blah blah blah blah up to the newline. keep keep keep keep keep i want to start deleting here blah blah blah +blah blah blah blah up to the newline.

    prints out:

    keep keep keep keep keep keep keep keep keep keep keep keep keep keep keep

    Since you are new to Perl, I will recommend reading through some of the online tutorials from Perl's official documentation website, starting with perlintro.

Re: Trying to delete text from a text file
by ww (Archbishop) on Jul 17, 2010 at 01:14 UTC
    Unclear: delete from "i want...\n" or from the word after "here" to the newline or something else?

    In any case, this appears (so far, anyway) to be a problem that could be tackled with a regular expression, so you may find your answer in perldoc perlretut or perldoc perlre either of which will teach you far more than you'll learn from being given a template for the necessary code.

    You may find some information about the Monastery and its norms helpful, too; please see On asking for help, How do I post a question effectively?, and I know what I mean. Why don't you?.

Re: Trying to delete text from a text file
by ahmad (Hermit) on Jul 17, 2010 at 01:08 UTC

    Welcome to PerlMonks,
    I advise you to try to make your own script that will do this job the if you face some difficulty we can help you out.

    Please read How do I post a question effectively?
Re: Trying to delete text from a text file
by johngg (Canon) on Jul 17, 2010 at 12:49 UTC

    Because you say that the phrase marking the "delete from" point is always the same, it might be more efficient to use substr and index instead of the regex solutions suggested by toolic and ww. I don't have time at the moment to benchmark though so you might want to run some comparisons for yourself.

    use strict; use warnings; use 5.010; my $deleteFromPhrase = q{i want to start deleting here}; while ( <DATA> ) { say substr $_, 0, index $_, $deleteFromPhrase; } __END__ keep keep keepi want to start deleting here blah blah no change in this para but change this onei want to start deleting here burble and also change this parai want to start deleting here blarf

    The output.

    keep keep keep no change in this para but change this one and also change this para

    I hope this is helpful.

    Cheers,

    JohnGG