Shell script for recursive directory lookup and line replacement

I need to recursively search directories and replace the string (say http: // development: port / URI ) with another (say http: // production: port / URI ) in all the files where it was found. Can anyone help?

It would be much better if the script could print the files that it modified and accept search / replace patterns as input parameters.

Sincerely.

+3
source share
5 answers

Try the following:

find . -type f | xargs grep -l development | xargs perl -i.bak -p -e 's(http://development)(http://production)g'

Another approach with a bit more feedback:

find . -type f | while read file
do
    grep development $file && echo "modifying $file" && perl -i.bak -p -e 's(http://development)(http://prodution)g' $file
done

Hope this helps.

+5
source

to find. -type f | xargs sed -is / pattern / replacement / g

+6
source

, . ( ?)

, . , .

+2

svn/cvs, .svn/.cvs, , . .svn, , find. - f | fgrep -v.svn | xargs sed -i 's/pattern/replacement/g'

+1

zsh, . :.

sed -i 's:pattern:target:g' ./**

0

Source: https://habr.com/ru/post/1697425/


All Articles