Search a text string and replace the whole line which contains it on a group of text files (Windows)

0

I'm looking for an automated solution than can search if a specified text string is on a line (may not be an exact match - just as long as the specified text string is contained in the line) on a group of text files and will remove the whole line (or lines) where the text string is/are found and replace it with a text string I specify. I am using Windows 7.

Reason for this: I edit AI files for an old strategy game (Total Annihilation). The AI files are basically text files, with a different AI file per map type. If I would like to make a particular edit the same in all the AI files, I would have to edit a lot of text files.

galacticninja

Posted 2010-11-07T08:15:17.600

Reputation: 5 348

Answers

2

Notepad++, http://notepad-plus-plus.org/ (also available as a portable app), will do a search and replace on files in the current directory and all subdirectories using regular expressions, in the search menu go to replace and then click on the "find in files" tab select the "regular expression" search mode type "^.your_string.$" in the search field and "your_new_string" in the "replace with" field.

better test it first.

Mohamed

Posted 2010-11-07T08:15:17.600

Reputation: 101

2

You can use sed in Windows. One source is GnuWin32. A typical command might look like this:

sed '/search pattern/s/^.*$/This is the new text/' file*.txt

which says "on every line in every file (named file[something].txt) that includes the text "search pattern", replace the whole line with "This is the new text".

More complex operations can be performed using regular expressions and other commands.

Paused until further notice.

Posted 2010-11-07T08:15:17.600

Reputation: 86 075

0

AutoIt can do this easily. You can look at this post for an example I wrote that does similar to what you need. The source I provided would only need minor tweaking to accommodate your needs, and could be made to do all the files at once.

MaQleod

Posted 2010-11-07T08:15:17.600

Reputation: 12 560