今天在Perl匹配替换的时候,发现有字串只能根据上一行定位,所以导致我要把2行都匹配出来,后做替换动作
开始这样写的:
perl -pi -e 's/something/repalceString/g' /Users/Swift/Desktop/config.plist
经过Google搜索在http://ask.xmodulo.com/search-and-replace-multi-line-string.html发现多一个-0的参数。
The "-i" option tells Perl to perform in-place editing, meaning that Perl reads the input file, makes substitutions, and writes the results back to the same input file. If you want to dry-run the changes, you can omit this option, in which case the results will be shown tostdoutwithout modifying the input file.
The "-0" option turns Perl into "file slurp" mode, where Perl reads the entire input file in one shot (intead of line by line). This enables multi-line search and replace.
The "-pe" option allows you to run Perl code (pattern matching and replacement in this case) and display output from the command line.
看man perl解释:
[-0[octal/hexadecimal] ]
加上-0:
perl -0pi -e 's/(?<=something).*?(?=\/\>\s)/repalceString/g' /Users/file.plist