2012-02-22 76 views
0

所以基本上我想明白爲什麼這個命令作爲單線發送到終端不能按預期工作。它運行幾分鐘,但包含「teststring1」的測試文件不會被替換。請不要從根本上改變語法或詢問爲什麼我從根開始做這件事,任何人都可以確定它沒有的原因嗎?從man sedFind/Sed沒有按預期工作

cd /tmp;find/-maxdepth 3 -type f -print0 | xargs -0 sed -i 's/teststring1/itworked!/gI' 
+0

殼牌解釋感嘆號,儘量不說? – ismail 2012-02-22 21:59:11

+0

現在正在嘗試,將在幾分鐘內更新結果 – user1166981 2012-02-22 22:00:30

+1

爲什麼要更改爲/ tmp,您的find命令使用'/'將搜索定位在根目錄。/tmp將被搜索,但是如果你打算搜索/ tmp及以下(你的-maxdepth 3),那麼可以使用'cd/tmp;找 。 -maxdepth ...'或者更簡單地說,'find/tmp -maxdepth ...',沒有最初的'cd/tmp;'。祝你好運。 – shellter 2012-02-22 22:03:06

回答

0

Citate:

If no -e, --expression, -f, or --file option is given, then the first non-option argument is taken as the sed script to interpret. All remaining arguments are names of input files; if no input files are specified, then the standard input is read. 

s/regular expression/replacement/flags 
The value of flags in the substitute function is zero or more of the following: 
- N  Make the substitution only for the N'th occurrence of the regular expression in the pattern space. 
- g  Make the substitution for all non-overlapping matches of the regular expression, not just the first one. 
- p  Write the pattern space to standard output if a replacement was made. If the replacement string is identical to that which it replaces, it is still considered to have been a replacement. 
- w file Append the pattern space to file if a replacement was made. If the replacement string is identical to that which it replaces, it is still considered to have been a replacement. 

所以find/-maxdepth 3 -type f -print0 | xargs -0 sed -e 's/[tT][eE][sS][tT][sS][tT][rR][iI][nN][gG]1/itworked!/g' -i將工作,只要你想。

如果你不喜歡不區分大小寫的比賽難看的模式,你可以使用Perl,而不是sed的:(!)find/-maxdepth 3 -type f -print0 | xargs -0 perl -pe 's/teststring1/itworked!/ig' -i