Using bash, I try to make a substitutions in files (replace B
with C
), but only in files which contain a certain string A
.
I tried
grep -Rl "A" | xargs sed -i 's/B/C'
But this fails when the filenames contain whitespaces
As an ugly workaround, I came up with the following solution which replaces the whitespaces with a placeholder :
for FILE in `grep -Rl "A" | tr " " "@"`;
do F1=`echo $FILE | tr "@" " "`;sed 's/B/C/' "$F1";
done
is there a more elegant solution?