Hi
Does anyone know of a free tool or a way of searching for a string and replacing it with another string in multiple files in one go?
Hi
Does anyone know of a free tool or a way of searching for a string and replacing it with another string in multiple files in one go?
That is a Unix type thing to do although you could try www.download.com to see if there is a dedicated Windows tool that can accomplish this as well. There is a Unix emulator for Windows called Cygwin that can run Unix commands within Windows. Unfortunately, I haven’t used Unix in ages and it isn’t intuitive or easy to remember. The entire command should take just one line. Maybe someone else knows how to do it.
Lots of text editors will do that for you.
Textpad does that and is free on windows.
But if you are a real man you will do it with sed like your forefathers did.
sed is widely available.
http://www.cornerstonemag.com/sed/
Sed - An Introduction and Tutorial is a tutorial simple search and replace is very near the beginning and is easy.
starting as a unix tool I am sure it comes with Macs these days.
Can’t say for sure, but I’d bet that Notepad++ can do that.
http://notepad-plus.sourceforge.net/uk/site.htm
textpad rocks. I use it all the time professionally.
I’m a little lost in the documentation…
How would I use it to replace ‘string 1’ with ‘string 2’ in all files *.sm in the current directory, overwriting the existing files?
It can; I’ve used it many times that way myself. Though I’d probably use sed if that is all I was trying to do.
Sorry… didn’t see you’d posted a tutorial… :smack:
sed s/current string/new string/g *.sm > *.sm.new
Then you can rename the .sm.new files to *.sm after you see that they are correct.
I would copy the *.sm files to a new directory and try this just to make sure you don’t mess up the originals while you get the script to work.
The first s mean substitute the g mean replace all occurrences not just the first.
I already did copy files to a temp folder. Tried the above command line
“The filename, directory name, or volume label syntax is incorrect.”
Edit: If I alter the line to write it all to a single file it works. It doesn’t seem to like using wildcards for the output file (or writing back to multiple files)
I already have Notepad++ so I tried using that instead.
I opened all the files (select them all then drag them into notepad++) then I did ‘replace in all open files’
As you’ve found, a decent text editor is probably the way to go. Sed would be the choice of those who know how to use it. I’d have gone with perl, myself; for your edification:
perl -p -i -e 's/search_text/replace_text/g' *.sm
Of course, search_text and replace_text can be replaced by regular expressions. And it should be noted that without being careful (e.g., consider escape characters and such), the one-liner above may lead to unexpected results and data loss.
That won’t work. The second * won’t magically match the first one (as much as we want it to), because argument expansion is dumb, and is done by the shell, not the program.
You need to do this:
find . -name “*.blah” -exec sed s/orig/replacement/g “{}” > “{}.new” ;
That will iterate through each file that matches *.blah" in your current directory, replace “orig” strings with “replacement” and copy the result in to *.blah.new (for each such file).