Batch Text Replacement

1

I need a tool that can open a text file and replace characters in a list with other characters in a list.

For example:

Lets say I have a list of "aA", "aB", and want to replace "aA" with "AA".

What is a software program that could do this easily?

nobody

Posted 2011-08-21T19:00:50.397

Reputation: 11

Virtually any scripting language can handle this easily: AutoIt, Python, Perl, etc. – MaQleod – 2011-08-21T19:02:18.267

I tried this with python but the logic has not been working properly so now I need a tool. – nobody – 2011-08-21T19:06:03.150

Try notepad++, you can do batch string replacement over many files: http://www.makeuseof.com/tag/how-to-find-and-replace-words-in-multiple-files/, or you can post your Python code and have a mod move this over to StackOverflow.com and they will help you out there.

– MaQleod – 2011-08-21T19:07:56.523

Answers

4

This is exactly what the sed program was made for.

psusi

Posted 2011-08-21T19:00:50.397

Reputation: 7 195

well, if he means change lowercase chars to uppercase, then perhaps not so easy with sed. if he means just changing some specifics, like aA to AA, then yes. – barlop – 2011-08-21T19:25:12.660

Or ex. Or indeed tr if using sed to change case is seen as hard (which, with GNU sed at least, it really isn't). – JdeBP – 2011-08-24T00:00:47.130

1

As a Perl one-liner, to replicate sed functionality...

perl -pe " s/aA/AA/g; s/aB/AB/g; " < input.txt > output.txt

This will turn this input.txt...

aA, aA, aA, aA, aA
aB, aB, aB, aB, aB

into this output.txt...

AA, AA, AA, AA, AA
AB, AB, AB, AB, AB

It does this through regular expression substitution...

s(ubstitute)/this-original-string/with-this-new-string/g(lobally)

Joe Internet

Posted 2011-08-21T19:00:50.397

Reputation: 5 145