How to do a recursive find/replace of a string with awk or sed?

后端 未结 30 1584
盖世英雄少女心
盖世英雄少女心 2020-11-22 06:39

How do I find and replace every occurrence of:

subdomainA.example.com

with

subdomainB.example.com

in eve

相关标签:
30条回答
  • 2020-11-22 07:26

    This one is compatible with git repositories, and a bit simpler:

    Linux:

    git grep -l 'original_text' | xargs sed -i 's/original_text/new_text/g'
    

    Mac:

    git grep -l 'original_text' | xargs sed -i '' -e 's/original_text/new_text/g'
    

    (Thanks to http://blog.jasonmeridth.com/posts/use-git-grep-to-replace-strings-in-files-in-your-git-repository/)

    0 讨论(0)
  • 2020-11-22 07:26

    For replace all occurrences in a git repository you can use:

    git ls-files -z | xargs -0 sed -i 's/subdomainA\.example\.com/subdomainB.example.com/g'
    

    See List files in local git repo? for other options to list all files in a repository. The -z options tells git to separate the file names with a zero byte, which assures that xargs (with the option -0) can separate filenames, even if they contain spaces or whatnot.

    0 讨论(0)
  • 2020-11-22 07:27

    Note: Do not run this command on a folder including a git repo - changes to .git could corrupt your git index.

    find /home/www/ -type f -exec \
        sed -i 's/subdomainA\.example\.com/subdomainB.example.com/g' {} +
    

    Compared to other answers here, this is simpler than most and uses sed instead of perl, which is what the original question asked for.

    0 讨论(0)
  • 2020-11-22 07:27
    cd /home/www && find . -type f -print0 |
      xargs -0 perl -i.bak -pe 's/subdomainA\.example\.com/subdomainB.example.com/g'
    
    0 讨论(0)
  • 2020-11-22 07:28

    For anyone using silver searcher (ag)

    ag SearchString -l0 | xargs -0 sed -i 's/SearchString/Replacement/g'
    

    Since ag ignores git/hg/svn file/folders by default, this is safe to run inside a repository.

    0 讨论(0)
  • 2020-11-22 07:29

    The simplest way for me is

    grep -rl oldtext . | xargs sed -i 's/oldtext/newtext/g'
    
    0 讨论(0)
提交回复
热议问题