I want to search for a string and replace it with another string recursively under a directory.
for example:
I want to search for a string
"var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www.");"
and replace it with
var _gaq = _gaq || [];_gaq.push(['_setAccount', 'UA-7044592-1']);_gaq.push(['_setDomainName', 'news4u.com']);_gaq.push(['_trackPageview']);
How to achieve it ?
Use
findto find all files, andxargsto runsedto modify them:that will find all files under dir and replace from with to, backing up the original files with the extension “.bak”
N.B. from is a basic regular expression, so you will need to escape any BRE metacharacters in the string you are searching for. It would be a good idea to test the sed command on a single file to get it working:
That will do the replacements to file but instead of modifying file it will write the output to test.out so you can check the results.