I have a file foo.txt

hello world
      first line to change
        second line to change

I would like to write a perl one-liner that changes foo.txt to read

hello world
      this is
        my new text

I tried

perl -p -i -e 's/      first line to change\n        second line to change/      this is\n        my new text/g' foo.txt

but this script does nothing to the file. What's wrong with my one-liner?


By default, perl read one line input at a time, so your regex never matches. For working with multiple lines input, you have two options.

Enable paragraph mode:

perl -i.bak -00pe ...

or slurp the whole file:

perl -i.bak -0777pe ...

(Any value above -0400 will cause perl to slurp the whole file, -0777 is used for convention)

|improve this answer|||||

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.