【问题标题】:How can I find & replace text in HTML, htm, PHP and txt files on a Linux server?如何在 Linux 服务器上查找和替换 HTML、htm、PHP 和 txt 文件中的文本?
【发布时间】:2012-12-03 15:42:18
【问题描述】:

我想知道如何在托管服务提供商的 Linux 服务器上查找和替换所有 HTML、htm、PHP 和 txt 扩展文件中的文本。我可以做 SSH。

待查文字http://mydomain.example.com

要替换的文本http://otherdomain.example.com/MyDomain

请给我准确的命令,如果能在更换前提示确认将非常有帮助。

【问题讨论】:

标签: linux sed


【解决方案1】:

这是使用sedfor 循环的一种方法。在覆盖前使用-i 标志和mv 进行提示:

for i in *.html *.htm *.php *.txt; do sed 's%\(http://www\.\)\(MyDomain\)\(\.com\)%\1OtherDomain\3/\2%g' "$i" > tmp && mv -i tmp "$i"; done

【讨论】:

    【解决方案2】:
    sed -i 's/http:\/\/www.mydomain.com/http:\/\/www.otherdomain.com\/MyDomain/gi' *.txt *.html *.htm *.php
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-11-10
      • 2020-07-04
      • 2020-07-29
      相关资源
      最近更新 更多