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

后端 未结 30 2141
盖世英雄少女心
盖世英雄少女心 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:39

    A bit old school but this worked on OS X.

    There are few trickeries:

    • Will only edit files with extension .sls under the current directory

    . must be escaped to ensure sed does not evaluate them as "any character"

    , is used as the sed delimiter instead of the usual /

    Also note this is to edit a Jinja template to pass a variable in the path of an import (but this is off topic).

    First, verify your sed command does what you want (this will only print the changes to stdout, it will not change the files):

    for file in $(find . -name *.sls -type f); do echo -e "\n$file: "; sed 's,foo\.bar,foo/bar/\"+baz+\"/,g' $file; done
    

    Edit the sed command as needed, once you are ready to make changes:

    for file in $(find . -name *.sls -type f); do echo -e "\n$file: "; sed -i '' 's,foo\.bar,foo/bar/\"+baz+\"/,g' $file; done
    

    Note the -i '' in the sed command, I did not want to create a backup of the original files (as explained in In-place edits with sed on OS X or in Robert Lujo's comment in this page).

    Happy seding folks!

提交回复
热议问题