Find / Sed无法按预期工作

时间:2012-02-22 21:55:07

标签: linux sed

所以基本上我想理解为什么这个作为单行发送到终端的命令不能按预期工作。它运行几分钟但我的测试文件包含“teststring1”没有被替换。请不要从根本上改变语法或问我为什么从root用户这样做,有人能找出原因吗?

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

1 个答案:

答案 0 :(得分:0)

来自man sed的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