I've got two variables VAR1 and VAR2 that contain strings. What I want to do is go through a list of files that have a .txt extension and change all occurences of VAR1 to VAR2. So far, it looks like this:
for i in `find . -name "*.txt"`
do
echo $i
sed -i -E "s|\$VAR1|\$VAR2|g" $i
done
I think everything except the sed line is working well. I think it's a syntax issue, but I haven't been able to figure out what it is. Any help would be appreciated Thanks
sedwill backup your files with the-Esuffix (soxyz.txtwill be backed up toxyz.txt-E). GNUsedwill handle things differently (the backup suffix, if it exists, must be attached to the-ioption). When you're not running into that issue, the-Eoption in Mac OS X (BSD)sedmeans 'use extended regular expressions' instead of 'sed-style basic regular expressions'. The normal equivalent option for GNUsedis-r.$VAR1with$VAR2only when there is a dollar sign in front. Is this correct? Please edit your question to clarify. Several of the answers here assume you want to interpolate the values of the shell variablesVAR1andVAR2into thesedscript instead.