且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

多行搜索替换为Perl

更新时间:2023-12-05 15:09:40

可以使用诸如-

perl -i -pe 's/START.*STOP/replace_string/g' file_to_change

有关完成同一件事的更多方法,请查看此线程.要处理多行搜索,请使用以下命令-

For more ways to accomplish the same thing check out this thread. To handle multi-line searches use the following command -

perl -i -pe 'BEGIN{undef $/;} s/START.*STOP/replace_string/smg' file_to_change

要将以下代码从单行代码转换为perl程序,请查看 perlrun文档.

In order to convert the following code from a one-liner to a perl program have a look at the perlrun documentation.

如果您真的有需要将其转换为工作程序,则只需让Perl为您处理文件打开/关闭.

If you really find the need to convert this into a working program then just let Perl handle the file opening/closing for you.

#!/usr/bin/perl -pi
#multi-line in place substitute - subs.pl
use strict;
use warnings;

BEGIN {undef $/;}

s/START.*STOP/replace_string/smg;

然后您可以使用文件名作为第一个参数调用脚本

You can then call the script with the filename as the first argument

$perl subs.pl file_to_change

如果您想要一个更丰富的脚本来处理文件打开/关闭操作(我们不喜欢所有那些"die"语句),那么请在-i [extension]下查看perlrun中的示例切换.

If you want a more meatier script where you get to handle the file open/close operations(don't we love all those 'die' statements) then have a look at the example in perlrun under the -i[extension] switch.