Linux 查找/搜索未按预期工作

Linux 查找/搜索未按预期工作,linux,sed,Linux,Sed,所以本质上我想理解为什么这个命令作为一行程序发送到终端不能按预期工作。它运行了几分钟,但是我的包含“teststring1”的测试文件没有被替换。请不要从根本上改变语法,也不要问我为什么要从根目录下执行此操作,有人能找出它不执行此操作的原因吗 cd /tmp;find / -maxdepth 3 -type f -print0 | xargs -0 sed -i 's/teststring1/itworked!/gI' 从人sed发起攻击: If no -e, --expression,

所以本质上我想理解为什么这个命令作为一行程序发送到终端不能按预期工作。它运行了几分钟,但是我的包含“teststring1”的测试文件没有被替换。请不要从根本上改变语法,也不要问我为什么要从根目录下执行此操作,有人能找出它不执行此操作的原因吗

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

人sed
发起攻击:

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-typef-print0 | xargs-0sed-e的/[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的/teststring1/itworked/ig'-i

Shell正在解释感叹号(!),请尝试不使用感叹号?好的,现在尝试,将在几分钟内更新结果为什么要麻烦更改为/tmp,您的find命令使用“/”将搜索定位在根目录/将搜索tmp,但如果您打算搜索/tmp及其以下(您的-maxdepth 3),则使用
cd/tmp;找到-maxdepth…
或者更简单地说,
find/tmp-maxdepth…
,没有首字母
cd/tmp。祝你好运。@Sheller-我想从“/”中搜索。我以前在其他目录下运行此命令时遇到过问题,所以我现在先将Cd刻录到/tmp。@Ismail,所以这并没有改变任何事情!出去,还有其他想法吗?