我希望有人可以解释为什么sed在perl为此问题所做的工作时不起作用:
$ egrep "(foo|bar)=0" /var/tmp/test.txt
This is example output of the test.txt file foo=0
This is another example output of the bar=0 test.txt file
$ sed -ir 's/(foo|bar)=0//g' /var/tmp/test.txt
$ egrep "(foo|bar)=0" /var/tmp/test.txt
This is example output of the test.txt file foo=0
This is another example output of the bar=0 test.txt file
$
Run Code Online (Sandbox Code Playgroud)
尝试用perl代替作品:
$ egrep "(foo|bar)=0" /var/tmp/test.txt
This is example output of the test.txt file foo=0
This is another example output of the bar=0 test.txt file
$ perl -ne 's/(foo|bar)=0//g;print;' -i /var/tmp/test.txt
$ egrep "(foo|bar)=0" /var/tmp/test.txt
$
Run Code Online (Sandbox Code Playgroud)
有没有办法让sed完成perl在这里做的事情?非常感谢你!
这只是您提供的参数顺序的问题sed.说sed -r -i,你会看到它的工作.
当您说sed -ir您正在设置就地编辑但不是-r模式时.为什么?因为-r被理解为参数-i,所以你最终得到了file+ r备份.
全面测试:
$ sed -ir 's/(foo|bar)=0//g' file
Run Code Online (Sandbox Code Playgroud)
file并且filer是平等的!
$ cat file
This is example output of the test.txt file foo=0
This is filenother example output of the bar=0 test.txt file
$ cat filer
This is example output of the test.txt file foo=0
This is filenother example output of the bar=0 test.txt file
Run Code Online (Sandbox Code Playgroud)
让我们分开参数:
$ sed -i -r 's/(foo|bar)=0//g' file
Run Code Online (Sandbox Code Playgroud)
现在好了,file不再有这个内容了:
$ cat file
This is example output of the test.txt file
This is filenother example output of the test.txt file
Run Code Online (Sandbox Code Playgroud)