【问题标题】:How do I recursively search and replace directory paths in terminal Mac?如何递归搜索和替换终端 Mac 中的目录路径?
【发布时间】:2013-09-24 00:43:21
【问题描述】:

我查看了以下推荐特定“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>

使用以下内容(将 localhost/massignition 替换为 www.site.com)

我输入以下内容:

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

但没有任何改变。

【问题讨论】:

    标签: macos replace sed terminal find


    【解决方案1】:

    删除命令中大括号前的 /,即

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

    而不是

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

    【讨论】:

    • 是的,我在发布后进行了调整,但不确定它是否有效,但看起来确实有效,谢谢。
    猜你喜欢
    • 2011-01-07
    • 1970-01-01
    • 2019-10-25
    • 2023-03-29
    • 1970-01-01
    • 2012-03-31
    • 1970-01-01
    • 1970-01-01
    • 2012-08-24
    相关资源
    最近更新 更多