by Mofi » Sun Sep 27, 2009 12:00 pm
Okay, the macro below was quickly written and is also very simple. You must have 2 files open when you run this macro, the file to be modified and the CSV file with the search and replace strings. The CSV file with the search and replace strings must have the focus when you execute the macro.
The search strings must be separated from the replace strings with a comma. It is possible that no replace string exists right the comma. In this case the searched string is just deleted.
The macro searches case sensitive for the search strings in the list and replaces all occurrences of the searched string by the replace string. Commas in search strings are not allowed and will result in a wrong selection of the search string. Commas in the replace string are no problem.
The macro as is copies first the entire list of search and replace strings into the other file at top of it. Then it inserts a line with marker character » which just marks where the list ends and where the content of file starts. No search string should start with that character or the macro would stop before the complete list of search/replace strings was processed.
In a loop with an indefinite number of runs first a check is made if the current line starts with the marker character » which results in exiting the loop. If this is not the case, a regular expression search is used marked from current cursor position (= start of current line) to next comma all characters and copy them to user clipboard 9. This is the search string. Next it sets the cursor right the comma character and selects everything to end of the current line. If nothing is selected, the macro runs the search with an empty replace string to delete all found strings. If a replace string is selected, the macro replaces case sensitive now all occurrences of the search string in clipboard 9 by the currently selected replace string. After every replace all the current line is deleted resulting in setting the cursor to start of the next line where the loop continues.
After the loop finished the line with the marker character » is deleted, user clipboard 9 is cleared and Windows clipboard is selected again.
The macro property Continue if search string not found must be checked for this macro.
InsertMode
ColumnModeOff
HexOff
UnixReOff
Clipboard 9
SelectAll
Copy
NextWindow
Top
Paste
IfColNumGt 1
"
»
"
Else
"»
"
EndIf
Top
Loop
IfCharIs "»"
ExitLoop
EndIf
Find RegExp "[~,^p]+"
Copy
EndSelect
Key RIGHT ARROW
StartSelect
Key END
EndSelect
IfSel
Find MatchCase "^c"
Replace All "^s"
Else
Find MatchCase "^c"
Replace All ""
EndIf
DeleteLine
EndLoop
DeleteLine
ClearClipboard
Clipboard 0
Please note: If one of the search strings is contained also in any other search or replace string below in the list, the macro as is would produce a wrong result. For example a list file like this:
search for,replace with
second search for word x,second replace with word y
would result in a wrong result because after processing the first line the second line of the list would have been changed also to
second replace with word x,second replace with word y
A modified macro would be required to handle such lists correct. Or you use a script which loads the search and replace strings at once into a string variable and runs the replaces using the strings in the variable. When the list of search/replace strings is long, a script would be definitely the better solution than using a macro. With a script you could also create in the output window a log for this job.