How to grep and replace

后端 未结 9 1606
囚心锁ツ
囚心锁ツ 2020-11-27 08:47

I need to recursively search for a specified string within all files and subdirectories within a directory and replace this string with another string.

I know that t

9条回答
  •  天命终不由人
    2020-11-27 09:42

    Another option would be to just use perl with globstar.

    Enabling shopt -s globstar in your .bashrc (or wherever) allows the ** glob pattern to match all sub-directories and files recursively.

    Thus using perl -pXe 's/SEARCH/REPLACE/g' -i ** will recursively replace SEARCH with REPLACE.

    The -X flag tells perl to "disable all warnings" - which means that it won't complain about directories.

    The globstar also allows you to do things like sed -i 's/SEARCH/REPLACE/g' **/*.ext if you wanted to replace SEARCH with REPLACE in all child files with the extension .ext.

提交回复
热议问题