我想用$ var替换一行。runnew有
input = input / old / should / change;replace = input / old / replace;other = input / old / other;replace_other = input / old / replace_other;我的输出文件应该……
你为什么要打电话 sed 什么?您可以直接在Perl中更轻松地处理您的要求:
sed
-i.bak
@ARGV
Perl自动负责打开文件,写入正确的文件并将旧文件重命名为 .bak 。
.bak
#!/usr/bin/perl -i.bak use warnings; use strict; my($replacement) = shift(@ARGV); while (<>) { s/input=.*/input= $replacement/; print; } exit 0;
测试运行(对输入数据进行有根据的猜测):
$ cat dummy1.txt.bak input= test1 input= test2 $ cat dummy2.txt.bak input= test3 input= test4 $ perl dummy.pl REPLACEMENT dummy1.txt dummy2.txt $ cat dummy1.txt input= REPLACEMENT input= REPLACEMENT $ cat dummy2.txt input= REPLACEMENT input= REPLACEMENT
或者使用文件“rerun.txt”的内容:
$ perl dummy.pl REPLACEMENT $(cat rerun.txt)
system() 将字符串列表作为其参数。您需要在传递它的命令周围加上引号。
system()
system ( "sed -i '/input=.*/c\input= $newinput' $runnew" );
但是你的代码看起来仍然很奇怪。你运行完全一样 sed 输入文件中每一行的命令。那是你的意思吗?
你在这里尝试做什么并不是很清楚。但我相信最好的方法会涉及到 的 不 强> 运用 sed 并使用Perl进行转换。