Find / Sed не работает должным образом - PullRequest
0 голосов
/ 23 февраля 2012

Так что, по сути, я хочу понять, почему эта команда, отправленная на терминал в виде однострочного текста, не работает так, как задумано.Он работает в течение нескольких минут, но мои тестовые файлы, содержащие "teststring1", не заменяются.Пожалуйста, не меняя радикально синтаксис и не спрашивая, почему я делаю это из-под root, может кто-нибудь определить причину, по которой это не так?

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

1 Ответ

0 голосов
/ 24 февраля 2012

Citate от man 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 -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

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...