Replacing string in all files found by grep. Can't get it to work
Typically, when you get a >
in the next line after hitting, it means that one of your quotes isn't closed yet. I couldn't find that mistake in your regex. But you do not need to surround the path /var/www_data/somepath/
with single quotes. I assume there are no unusual characters in somepath
?
Anyways, I tested your regex with sed. \d\w
look like vim
syntax for me, that's why I translated it to ascii (which always works). Also, inside of []
you do not need to escape .
:
sed -r "s/'([A-Za-z0-9_-.]+)(@domain.com)'/'adsf'/g" test.dat
Indeed you can use sed
or perl
for your task. You don't necessarily need grep
to generate a file list, unless you have GB of data. Then presorting could result in a speed benefit.
To test your regex, you could do the following:
cd /var/www_data/somepath/
sed -r 's|pattern|replace-pattern|g' a_single_file.php
When you're satisfied with the result, just add the -ibak
(--in-place=bak
) argument and run it on all files
find . -type f -name '*.php' -o -name '*.ini' -o name '*.conf' -o -name '*.sh' \
-exec sed -r -ibak 's|pattern|replace-pattern|g' '{}' \;
The original files are being put into <orignalname.php>.bak
.
To answer your last question. For this job, grep
is the tool you want, you could run it on the .bak
files generated by sed above:
grep --recursive --include='*.bak' -E --files-with-matches 'pattern' . > files_fixed.txt
or, simply:
find . -type f -name '*.bak'
With GNU sed
you can use sed -i
.
sed -i 'script' *.{php,ini,conf,sh}