Sed is plain horrible, so I've been putting together a small shell script so I can write it once and never have to write it again. (I don't want to use sed)
This is what I have so far
echo -e "OLD string - ctrl + d when done: \n"
oldstring="$(cat)"
echo -e "NEW string - ctrl + d when done: \n"
newstring="$(cat)"
echo -e "Renaming all instances of: \n\n$oldstring\n\n to: \n\n$newstring"
read -p "Is this correct (y/n) " -n 1 -r
echo ""
echo "Beginning changes"
if [[ $REPLY =~ ^[Yy]$ ]]
then
find . -type f -name '*.js' -exec sed -i "" "s@$oldstring@$newstring@g" {} +
echo "Done"
fi
The wonderful side of this is that it works for simple matches, like replacing "name" with "bob".
Problem is when I want to paste in multi-line code snippets, for example input:
return this.executeFn(params).then(results => {
resultStore[name] = results.result;
return results;
});
Replace with:
return this.executeFn(params).then(
results => {console.log(results);}
);
I'm guessing I need to somehow automagically escape all characters in oldstring & newstring, so sed understands the before & after?
I'm open to other tools, if there's an easier way. I just want to avoid having to edit ±300 files for changes like this.
The "duplicate" sed tag doesn't help. Sed was just one option I'm exploring, and the answers in the other thread suggest to manually edit and escape parts of a string. Not an option for the bash script.