gpt4 book ai didi

linux - 仅打开包含特定字符串的文件,然后在 Linux 命令行上替换

转载 作者:太空狗 更新时间:2023-10-29 11:45:42 26 4
gpt4 key购买 nike

我一直在使用一个简单的查找命令来搜索数百个 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

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

提前感谢您的帮助。

最佳答案

你可以试试这个:

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

关于linux - 仅打开包含特定字符串的文件,然后在 Linux 命令行上替换,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17616613/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com