如何在终端Mac中递归搜索和替换目录路径?

时间:2013-09-19 04:13:01

标签: macos replace sed terminal find

我查看了以下推荐特定'sed'命令的其他线程:

https://superuser.com/questions/428493/how-can-i-do-a-recursive-find-and-replace-from-the-command-line

Find/sed: How can I recursively search/replace a string in files but only for lines that match a particular regexp

Recursive search and replace in text files on Mac and Linux

perl -pi -w -e 's/SEARCH_FOR/REPLACE_WITH/g;' *.txt

find . -type f -name '*.txt' -exec sed -i '' s/this/that/ {} +

我正在尝试替换目录中多个文件中出现的以下示例:

<script src="http://localhost/massignition/wp-content/themes/twentythirteen/js/html5.js"></script>

以下内容(用www.site.com代替localhost / massignition)

我键入以下内容:

find . -type f -name '*.txt' -exec sed -i '' s%localhost/massignition%www.site.com%/ {} +

但没有任何改变。

1 个答案:

答案 0 :(得分:1)

删除/在命令中的花括号之前存在,即

找到。 -type f -name'* .txt'-exec sed -i'的%localhost / massignition%www.site.com%{} +

而不是

找到。 -type f -name'* .txt'-exec sed -i'的%localhost / massignition%www.site.com%/ {} +