Multi Search-n-Replace! Possible through lists???

General questions about using TextPad

Moderators: AmigoJack, bbadmin, helios, Bob Hansen, MudGuard

Post Reply
vamshireddy007
Posts: 5
Joined: Fri Sep 19, 2003 2:22 am

Multi Search-n-Replace! Possible through lists???

Post by vamshireddy007 »

------------ Reposting this question in this group ------------
Hello Everyone,

Is it possible in textpad to make it look for a list of {REPLACE_FROM, REPLACE_TO} strings and perform multiple search and replace in a specific document

For example:
(a) If file initially looks as below:

----------- File_BGN -----------
String01, String02, String03
String02, String03, String04
String03, String04, String05
----------- File_END -----------

(b) I have Search-n-Replace sets as below

----------- Search-n-Replace set BGN -----------
String01,String_A
String02,String_B
String03,String_C
String04,String_D
String05,String_E
----------- Search-n-Replace set END -----------

Search-n-Replace strings can be either stored in a file or in clipboard (?).

(a) The resultant file should look as below:

----------- File_BGN -----------
String_A, String_B, String_C
String_B, String_C, String_D
String_C, String_D, String_E
----------- File_END -----------

Any :idea: ideas/thoughts/suggestions folks?

Cheers,
Gurram
User avatar
talleyrand
Posts: 624
Joined: Mon Jul 21, 2003 6:56 pm
Location: Kansas City, MO, USA
Contact:

Post by talleyrand »

Install Python and you're good to go.

Copy this code and paste into Textpad.
Save it to something like C:\vamshireddy007.py
Update the path for file and the mapping of what you want to search and replace on.

C:\>python vamshireddy007.py

Will turn
---- begin before ----
String01, String02, String03
String02, String03, String04
String03, String04, String05
---- end before ----
into
---- begin after ----
String_A, String_B, String_C
String_B, String_C, String04
String_C, String04, String05
---- end after ----

You can also run it straight out of Texpad. Set the command to wherever you installed Python (probably C:\python23\python.exe)

Code: Select all

def searchAndReplace(fileName, keywordMappings):
   """
   fileName is the fully qualified path to a file that we have permissions to edit.
   keywordMappings is a dictionary with a one to one mapping between keyword and
      value.
   """

   #create an empty list
   out = []

   #if using Python 2.2 or earlier, uncomment the following lines
   #True = 1
   #False = 0

   #Set this to False if you'd like to force it to lowercase for matching
   isCaseSensitive = True

   #open the file for reading and iterate through each line
   for currentLine in open(fileName, 'r').readlines():

      # For each thing in our mapping, see if it exists in the current line
      for key, value in keywordMappings.items():

         #Text searching is case sensitive by default so to force everything to
         # lowercase to get a match
         if (not isCaseSensitive):
            currentLine = currentLine.lower()
            key = key.lower()
            value = value.lower()

         #Perform a replacement anywhere in currentline where key exists with value
         currentLine = currentLine.replace(key, value)

      out.append(currentLine)

   #save the file with a different name
   #remove the + '.txt' if you'd like to overwrite the file
   output = open(fileName + '.txt', 'w')
   output.writelines(out)


def main():
   """
   """
   #there may be issues with spaces in the file or directory path
   # I haven't had the time to dig into it.  Worst case scenario,
   # save this file into the current directory as the file you are
   # wanting to Search and Replace on
   file = "f:\python\source.txt"

   #Define our mappings here
   #Create an empty dictionary
   d = {}
   #add new mappings like this
   #Strings can be denoted with either single or double quotes
   d['String01'] = "String_A"
   d['String02'] = "String_B"
   d['String03'] = "String_C"

   searchAndReplace(file, d)


if __name__ == '__main__':
   main()
I choose to fight with a sack of angry cats.
vamshireddy007
Posts: 5
Joined: Fri Sep 19, 2003 2:22 am

Multi Search-n-Replace! Possible through lists???

Post by vamshireddy007 »

Hi talleyrand,
Thanks for the "Phyton" solution for my problem.

I didn't know Python before. Will start learning.

I tried running the program in TP and it worked well.

Is there any way we can call this program from the data file window where these replacements are supposed to take?

I'm rather looking for a generic solution.

Thanks again for the help.

Cheers,
Gurram
User avatar
trids
Posts: 69
Joined: Wed May 07, 2003 10:16 am
Location: South Africa

Post by trids »

Another option is to use AutoIt3 (a Windows scripting/macro tool) .. which I'm exploring right now, and am having lots of fun integrating it with TextPad :)
User avatar
trids
Posts: 69
Joined: Wed May 07, 2003 10:16 am
Location: South Africa

Post by trids »

Or you can use the commandline utility,sed from http://gnuwin32.sourceforge.net/packages/sed.htm as follows .. Save the following script as mysed.sed:

Code: Select all

s/String01/String_A/g
s/String02/String_B/g
s/String03/String_C/g
s/String04/String_D/g
s/String05/String_E/g
Then run the following command at the DOS prompt:

Code: Select all

sed -f mysed.sed file_in.txt>file_out.txt
There are many ways to use sed, and it is a brilliant partner for TextPad. Try it and you'll love it!

8)
Post Reply