更新时间:2023-01-15 21:05:12
你的意思是搜索和替换通过的grep匹配所有文件的字符串?
Do you mean search and replace a string in all files matched by grep?
perl -p -i -e 's/oldstring/newstring/g' `grep -ril searchpattern *`
修改
由于这似乎是一个相当热门的一个问题想我会更新。
Since this seems to be a fairly popular question thought I'd update.
现在我主要使用 ACK-的grep
,因为它更方便用户使用。所以上面的命令是:
Nowadays I mostly use ack-grep
as it's more user-friendly. So the above command would be:
perl -p -i -e 's/old/new/g' `ack -l searchpattern`
要在文件名中处理空格您可以运行:
To handle whitespace in file names you can run:
ack --print0 -l searchpattern | xargs -0 perl -p -i -e 's/old/new/g'
您可以用 ACK-的grep
做多。假设你想将搜索限制HTML文件只:
you can do more with ack-grep
. Say you want to restrict the search to HTML files only:
ack --print0 --html -l searchpattern | xargs -0 perl -p -i -e 's/old/new/g'
如果白色空间不是它甚至更短的一个问题:
And if white space is not an issue it's even shorter:
perl -p -i -e 's/old/new/g' `ack -l --html searchpattern`
perl -p -i -e 's/old/new/g' `ack -f --html` # will match all html files