in reply to grep and delete

If the files aren't huge, or the word appears fairly early (a few hundred MB) in the file, then you can do this with a "one-liner" (wrapped for viewing; adjust quoting to your OS needs):

perl -e"BEGIN{$/=qq[the word or phrase]}" -ne"$n=tell ARGV; close ARGV; truncate $ARGV, $n" path\to\the\file

Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
Lingua non convalesco, consenesco et abolesco. -- Rule 1 has a caveat! -- Who broke the cabal?
"Science is about questioning the status quo. Questioning authority".
In the absence of evidence, opinion is indistinguishable from prejudice.

Replies are listed 'Best First'.
Re^2: grep and delete
by davido (Cardinal) on Jan 21, 2006 at 06:25 UTC

    Here's another one-liner that doesn't use a begin block:

    perl -ni.bak -e "print; /word/ && last;" file.txt

    When the thought came to me, I was surprised at how simple the solution turned out to be.


    Dave

      That doesn't quite match the OPs stated requirements in that it will preserve the entire 'line', (which could be the entire file if this was a binary file), containing the keyword rather than truncating immediately after the keyword.

      Has the advantage that it does matter how big the file (assuming non-binary), or how far into the file the truncation point is.

      Has the disadvantage that it will consume more disc space rather than reducing it--if that was the original intent.


      Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
      Lingua non convalesco, consenesco et abolesco. -- Rule 1 has a caveat! -- Who broke the cabal?
      "Science is about questioning the status quo. Questioning authority".
      In the absence of evidence, opinion is indistinguishable from prejudice.

        I think it does match the OP's spec: He said he wanted to truncate the lines below the word, ie the next line and beyond. Maybe I'm misreading it, but that's how it sounded to me. And the fact that he is speaking of lines leads me to believe that he is dealing with a file that has line endings.


        Dave