2015-11-04 51 views
1

我希望有人可以解釋爲什麼SED的地方不工作perl做針對此問題:VS Perl的正則表達式的sed substitue /(FOO |酒吧)/

$ 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 
$ 

用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 
$ 

有沒有辦法讓sed完成perl在這裏的工作?非常感謝你!

回答

6

這只是您提供給sed的參數順序上的一個問題。說sed -r -i,你會看到它的工作。

當你說sed -ir你正在設置就地編輯,但不是-r模式。爲什麼?因爲-r被理解爲-i的參數,所以您最終擁有一個file + r備份。


完全測試:

$ sed -ir 's/(foo|bar)=0//g' file 

filefiler都是平等的!

$ 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 

讓我們分開參數:

$ sed -i -r 's/(foo|bar)=0//g' file 

現在它是好的,file沒有這個內容有任何更多:

$ cat file       
This is example output of the test.txt file 
This is filenother example output of the test.txt file 
+3

同'perl的-in -e」。 ..「,例如。 – ThisSuitIsBlackNot