2

我一直在使用一个简单的 find 命令来搜索数百个 html 文件,然后在每个文件中替换一些简单的文本。

  1. 查找并列出包含搜索字符串的文件。

    find . -iname '*php' | xargs grep 'search-string' -sl
    

这给了我一个简单的文件列表,例如。

    ./javascript_open_new_window_form.php
    ./excel_large_number_error.php
    ./linux_vi_string_substitution.php
    ./email_reformat.php
    ./online_email_reformat.php
  1. 搜索和替换我使用的字符串。

    sed -i 's/search-string/replace-string/' ./javascript_open_new_window_form.php
    sed -i 's/search-string/replace-string/' ./excel_large_number_error.php
    sed -i 's/search-string/replace-string/' ./linux_vi_string_substitution.php
    sed -i 's/search-string/replace-string/' ./email_reformat.php
    sed -i 's/search-string/replace-string/' ./online_email_reformat.php
    

所以我的问题是......如何组合这两个命令,这样我就不必每次都手动复制和粘贴文件名。

提前感谢您的帮助。

4

2 回答 2

3

你可以试试这个:

find . -iname '*php' | xargs grep 'search-string' -sl | while read x; do echo $x; sed -i 's/search-string/replace-string/' $x; done
于 2013-07-12T14:18:02.500 回答
1

将其再次通过管道传输到另一个xargs. 只需第二次xargs使用-n 1,为输入中的每个文件一个一个地运行命令,而不是默认行为。像这样:

find . -iname '*php' | xargs grep 'search-string' -sl | xargs -n 1 sed -i 's/search-string/replace-string/'
于 2013-07-12T19:34:35.590 回答