我想将几个单行内容移动到一个脚本中.
例如:
perl -i.bak -pE "s/String_ABC/String_XYZ/g" Cities.Txt
perl -i.bak -pE "s/Manhattan/New_England/g" Cities.Txt
Run Code Online (Sandbox Code Playgroud)
以上对我来说效果很好,但代价是两个磁盘I/O操作.
我想将上述逻辑移动到一个脚本中,以便所有替换都在文件打开和编辑一次的情况下完成.
编辑1:根据您的建议,我在一个脚本中编写了这个片段,当从Windows批处理文件调用时,它只是挂起:
#!/usr/bin/perl -i.bak -p Cities.Txt
use strict;
use warnings;
while( <> ){
s/String_ABC/String_XYZ/g;
s/Manhattan/New_England/g;
print;
}
Run Code Online (Sandbox Code Playgroud)
编辑2:好的,所以这是我实施你的建议的方式.奇迹般有效!
批处理文件:
perl -i.bal MyScript.pl Cities.Txt
Run Code Online (Sandbox Code Playgroud)
MyScript.pl
#!/usr/bin/perl
use strict;
use warnings;
while( <> ){
s/String_ABC/String_XYZ/g;
s/Manhattan/New_England/g;
print;
}
Run Code Online (Sandbox Code Playgroud)
非常感谢所有贡献的人.