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

Hi All, I have a folder containing different files and want to delete a line containing a string from the files. I did this in one line. Can anyone please suggest how can I use the same in perl script.
perl -i -nle'/apple/||print' items/fruits

Thanks

Replies are listed 'Best First'.
Re: Delete a line from files
by AnomalousMonk (Archbishop) on Oct 12, 2015 at 01:44 UTC

    Further to AnonyMonk's advice above, also consult perlrun about the effects of the -i, -n and -l flags; they all act to add code around the code you supply on the command line.

    Update: E.g.,

    c:\@Work\Perl\monks>perl -MO=Deparse,-p -i -n -le "/apple/||print " BEGIN { $^I = ""; } BEGIN { $/ = "\n"; $\ = "\n"; } LINE: while (defined(($_ = <ARGV>))) { chomp($_); (/apple/ or print($_)); } -e syntax OK
    See O and B::Deparse.


    Give a man a fish:  <%-{-{-{-<

      what is the purpose of the BEGIN blocks?
        "...the purpose of the BEGIN blocks?"

        Begin to read?

        Update: Slight losses are usual in every combat.

        Regards, Karl

        «The Crux of the Biscuit is the Apostrophe»

Re: Delete a line from files
by Anonymous Monk on Oct 11, 2015 at 23:38 UTC
    read perlintro (and read it well, from the beginning), read when it talks about creating and initializing variables with my, opening files with open/binmode, reading the files in a while loop with readline, print strings to new file handle, and testing strings with m//atch operator in Simple matching/perlrequick

    Editing a file is fours steps

    • read original-file
    • modify data
    • write new-file
    • rename new-file to original-file

    And maybe read Path::Tiny if you like convenience methods