2013-07-12 41 views
2

我一直在使用簡單的find命令來搜索數百個html文件,然後替換每個文件中的一些簡單文本。只打開包含特定字符串的文件,然後在Linux命令行上替換

  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 
    

所以我的問題是...我怎麼能結合兩個命令,所以我不必須手動複製和每次粘貼文件名。

感謝您的幫助提前。再次

find . -iname '*php' | xargs grep 'search-string' -sl | while read x; do echo $x; sed -i 's/search-string/replace-string/' $x; done 

回答

3

你可以試試這個。只需讓第二個xargs使用-n 1爲輸入中的每個文件逐個運行命令,而不是默認行爲。像這樣:

find . -iname '*php' | xargs grep 'search-string' -sl | xargs -n 1 sed -i 's/search-string/replace-string/' 
1

管到另一個xargs

相關問題