# Tool or way of doing find and replace in multiple text files?

**URL:** <https://boards.straightdope.com/t/tool-or-way-of-doing-find-and-replace-in-multiple-text-files/452464>\
**Category:** Factual Questions\
**Created:** [June 10, 2008, 4:57pm UTC](https://boards.straightdope.com/t/tool-or-way-of-doing-find-and-replace-in-multiple-text-files/452464 "2008-06-10T16:57:18Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![Lobsang](https://sea3.discourse-cdn.com/straightdope/user_avatar/boards.straightdope.com/lobsang/32/4067_2.png) [@Lobsang](https://boards.straightdope.com/u/Lobsang)\
**Post date:** [June 10, 2008, 4:57pm UTC](https://boards.straightdope.com/t/tool-or-way-of-doing-find-and-replace-in-multiple-text-files/452464/1 "2008-06-10T16:57:18Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![Shagnasty](https://avatars.discourse-cdn.com/v4/letter/s/9dc877/32.png) [@Shagnasty](https://boards.straightdope.com/u/Shagnasty)\
**Post date:** [June 10, 2008, 5:11pm UTC](https://boards.straightdope.com/t/tool-or-way-of-doing-find-and-replace-in-multiple-text-files/452464/2 "2008-06-10T17:11:29Z")

</div>

That is a Unix type thing to do although you could try [www.download.com](http://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](http://en.wikipedia.org/wiki/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.

---

<div class="post-metadata">

**Author:** ![gazpacho](https://avatars.discourse-cdn.com/v4/letter/g/6f9a4e/32.png) [@gazpacho](https://boards.straightdope.com/u/gazpacho)\
**Post date:** [June 10, 2008, 5:20pm UTC](https://boards.straightdope.com/t/tool-or-way-of-doing-find-and-replace-in-multiple-text-files/452464/3 "2008-06-10T17:20:21Z")

</div>

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/](http://www.cornerstonemag.com/sed/)  
[Sed - An Introduction and Tutorial](http://www.grymoire.com/Unix/Sed.html) 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.

---

<div class="post-metadata">

**Author:** ![Revtim](https://sea3.discourse-cdn.com/straightdope/user_avatar/boards.straightdope.com/revtim/32/1042_2.png) [@Revtim](https://boards.straightdope.com/u/Revtim)\
**Post date:** [June 10, 2008, 5:36pm UTC](https://boards.straightdope.com/t/tool-or-way-of-doing-find-and-replace-in-multiple-text-files/452464/4 "2008-06-10T17:36:06Z")

</div>

Can’t say for sure, but I’d bet that Notepad++ can do that.  
[http://notepad-plus.sourceforge.net/uk/site.htm](http://notepad-plus.sourceforge.net/uk/site.htm)

---

<div class="post-metadata">

**Author:** ![Enright3](https://sea3.discourse-cdn.com/straightdope/user_avatar/boards.straightdope.com/enright3/32/2897_2.png) [@Enright3](https://boards.straightdope.com/u/Enright3)\
**Post date:** [June 10, 2008, 5:50pm UTC](https://boards.straightdope.com/t/tool-or-way-of-doing-find-and-replace-in-multiple-text-files/452464/5 "2008-06-10T17:50:17Z")

</div>

textpad rocks. I use it all the time professionally.

---

<div class="post-metadata">

**Author:** ![Lobsang](https://sea3.discourse-cdn.com/straightdope/user_avatar/boards.straightdope.com/lobsang/32/4067_2.png) [@Lobsang](https://boards.straightdope.com/u/Lobsang)\
**Post date:** [June 10, 2008, 5:58pm UTC](https://boards.straightdope.com/t/tool-or-way-of-doing-find-and-replace-in-multiple-text-files/452464/6 "2008-06-10T17:58:00Z")

</div>

[QUOTE=gazpacho]  
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/](http://www.cornerstonemag.com/sed/)  
[Sed - An Introduction and Tutorial](http://www.grymoire.com/Unix/Sed.html) 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.  
[/QUOTE]

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?

---

<div class="post-metadata">

**Author:** ![tanstaafl](https://avatars.discourse-cdn.com/v4/letter/t/ba8739/32.png) [@tanstaafl](https://boards.straightdope.com/u/tanstaafl)\
**Post date:** [June 10, 2008, 6:04pm UTC](https://boards.straightdope.com/t/tool-or-way-of-doing-find-and-replace-in-multiple-text-files/452464/7 "2008-06-10T18:04:48Z")

</div>

[QUOTE=Revtim]  
Can’t say for sure, but I’d bet that Notepad++ can do that.  
[http://notepad-plus.sourceforge.net/uk/site.htm](http://notepad-plus.sourceforge.net/uk/site.htm)  
[/QUOTE]

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.

---

<div class="post-metadata">

**Author:** ![Lobsang](https://sea3.discourse-cdn.com/straightdope/user_avatar/boards.straightdope.com/lobsang/32/4067_2.png) [@Lobsang](https://boards.straightdope.com/u/Lobsang)\
**Post date:** [June 10, 2008, 6:07pm UTC](https://boards.straightdope.com/t/tool-or-way-of-doing-find-and-replace-in-multiple-text-files/452464/8 "2008-06-10T18:07:04Z")

</div>

[QUOTE=Lobsang]  
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?  
[/QUOTE]

Sorry.. didn’t see you’d posted a tutorial.. :smack:

---

<div class="post-metadata">

**Author:** ![gazpacho](https://avatars.discourse-cdn.com/v4/letter/g/6f9a4e/32.png) [@gazpacho](https://boards.straightdope.com/u/gazpacho)\
**Post date:** [June 10, 2008, 6:13pm UTC](https://boards.straightdope.com/t/tool-or-way-of-doing-find-and-replace-in-multiple-text-files/452464/9 "2008-06-10T18:13:03Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Lobsang](https://sea3.discourse-cdn.com/straightdope/user_avatar/boards.straightdope.com/lobsang/32/4067_2.png) [@Lobsang](https://boards.straightdope.com/u/Lobsang)\
**Post date:** [June 10, 2008, 6:25pm UTC](https://boards.straightdope.com/t/tool-or-way-of-doing-find-and-replace-in-multiple-text-files/452464/10 "2008-06-10T18:25:47Z")

</div>

[QUOTE=gazpacho]  
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.  
[/QUOTE]

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)

---

<div class="post-metadata">

**Author:** ![Lobsang](https://sea3.discourse-cdn.com/straightdope/user_avatar/boards.straightdope.com/lobsang/32/4067_2.png) [@Lobsang](https://boards.straightdope.com/u/Lobsang)\
**Post date:** [June 10, 2008, 6:39pm UTC](https://boards.straightdope.com/t/tool-or-way-of-doing-find-and-replace-in-multiple-text-files/452464/11 "2008-06-10T18:39:26Z")

</div>

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’

---

<div class="post-metadata">

**Author:** ![Digital\_Stimulus](https://avatars.discourse-cdn.com/v4/letter/d/aeb1de/32.png) [@Digital\_Stimulus](https://boards.straightdope.com/u/Digital_Stimulus)\
**Post date:** [June 11, 2008, 12:12am UTC](https://boards.straightdope.com/t/tool-or-way-of-doing-find-and-replace-in-multiple-text-files/452464/12 "2008-06-11T00:12:43Z")

</div>

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:

```auto

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.

---

<div class="post-metadata">

**Author:** ![iamthewalrus\_3](https://avatars.discourse-cdn.com/v4/letter/i/258eb7/32.png) [@iamthewalrus\_3](https://boards.straightdope.com/u/iamthewalrus_3)\
**Post date:** [June 11, 2008, 12:21am UTC](https://boards.straightdope.com/t/tool-or-way-of-doing-find-and-replace-in-multiple-text-files/452464/13 "2008-06-11T00:21:00Z")

</div>

[QUOTE=gazpacho]  
sed s/current string/new string/g \*.sm \> \*.sm.new  
[/QUOTE]  
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).
