Automatic Text replace with a list

Hi,
I am looking for a way to replace a word in some text with a list of words, thus creating several new files with that text one change in each file.
For example Have some text that says: “USA is the best” and replace USA with a list of every state in the United states creating a new text file for each state.
So it would say “Alabama is the best.” That file would be saved a new one would be created that says “alaska is the best” and go through every state in the union. for example. in the end there would be 50 files with USA changed to their specific state.
Would be also great if the files that were saved could be named after the word that was used to replace. so instead of USA.html it would be alabama.html etc…
Is that possible?

Thanks!

Hm. You could do a mail-merge in Word/Excel to accomplish this, but I don’t know if you could save each one to a different file. (Usually you print each one out-- think printing 500 invitations, each with a different name on them.)

A quick Google shows you can indeed tell Word to save each document instead of printing it: http://office.microsoft.com/en-us/word-help/mail-merge-step-4-preview-the-merge-and-then-complete-it-HA001109552.aspx?CTT=5&origin=HA001034920 Try following those instructions and see if they work for you.

This is something that can be scripted fairly easily.

What operating system are you using?

Thing is, if you’re really doing 50 states it might take less time to just do 50 search & replaces rather than learning how to do mail-merge. It certainly would me. Now if you’re doing 254 Texas counties, well maybe.

You can do this sort of thing, at a pinch, with good old Windows batch files, although I wouldn’t recommend it, because they are weird and unpredictable. In this case:


@echo off
SETLOCAL ENABLEDELAYEDEXPANSION 
for %%a in (Alabama California Texas) do (
 set result=
 for %%b in (USA is the best) do (
  if "%%b"=="USA" (
   set result=!result! %%a
  ) else (
   set result=!result! %%b
  )
 )
 echo !result!> %%a.html
)

Save that in a file called something like replace.cmd and then run it. The files will be created in the same directory, mind.