Perl command line multi-line replace

后端 未结 2 1085
慢半拍i
慢半拍i 2020-11-29 05:16

I\'m trying to replace text in a multi-line file using command-line perl. I\'m using Ubuntu Natty.

Below is the content of my text file (called test.txt):

         


        
2条回答
  •  时光说笑
    2020-11-29 05:56

    You are reading the file line-by-line, so only the first line matches your regex. What you'll want to do -- if you truly wish to delete most of the content -- is to slurp the file by using the -0 option, e.g. -0777. This is line ending processing, and 777 is just a number used by convention as an octal number large enough so as to cause file slurping.

    perl -0777 -i -pe 's/(\[mysqld\][^\^]+)/$1\nsometext/g' test.txt
    

    Also, I replaced your quotes. If you are in *nix, which it seems you are, single quotes are preferable. Case in point, $1 would not be interpolated by the shell.

提交回复
热议问题