Script for search and replace for text between tags/markers

May 6, 2012 at 12:24:46
Specs: Windows 7
Hi,
I have a text file which will typically be between 10k and 50k.
I want to do a number of searches and replaces that are listed in another csv file.

For example, the csv listing the search and replace value is called replacement.csv with the first column showing the value to search for and the second column with the value to replace it with. For example,

TextPattern1,NewPattern1
TextPattern2,NewPattern2

The second file contains the data I want to search and replace. Specifically, I want to search only those lines that start with <NAME>. These lines end with a CR/LF. If a match to one of the patterns is found, I want to replace all the text between the <NAME> tag and the CR/LF. For example, here is an example of the file I would start with:

<ID>1
<NAME>XXXXXXX 123456789 TextPattern1 xxx
<FIELD3>Data
<FIELD4>More Data
<FIELD5>Even More Data
<ID>2
<NAME>XXXXXXX 26789 NotthePattern xxx
<FIELD3>Data
<FIELD4>More Data
<FIELD5>Even More Data
<ID>3
<NAME>XXXXXXX 126789 TextPattern2 XXXXXXX 126789
<FIELD3>Data
<FIELD4>More Data
<FIELD5>Even More Data

Once I run the process, I want to the file to look like this:
<ID>1
<NAME>NewPattern1
<FIELD3>Data
<FIELD4>More Data
<FIELD5>Even More Data
<ID>2
<NAME>XXXXXXX 26789 NotthePattern xxx
<FIELD3>Data
<FIELD4>More Data
<FIELD5>Even More Data
<ID>3
<NAME>NewPattern2
<FIELD3>Data
<FIELD4>More Data
<FIELD5>Even More Data

I am only just starting to look at Powershell for this as I have read in a number of places this is the best tool to use for stuff like this, although I am open to other tool suggestions. Any help give will be greatly appreciated.

Thanks,
Jbarbara11


See More: Script for search and replace for text between tags/markers

Report •

#1
July 1, 2012 at 21:18:41
Here's a rough prototype, but it WILL need tweaked, probably overhauled:

@echo off & setlocal enabledelayedexpansion
for /f "tokens=1,2 delims=," %%a in (replacement.csv) do (
for /f "tokens=1* delims=<>" %%y in (fu.uu) do (
if "%%y" equ "NAME" (
set test=%%z
set test2=!test:%%a=!
if "!test2!" neq "!test!" echo REPL**** test2:!test2!] test:!test!] B:%%b
if !test2! neq !test! (>> newfile echo ^<NAME^>%%b) else (>> newfile echo ^<%%y^>%%z)
) else (
>> newfile echo ^<%%y^>%%z
)
)
move /y newfile fu.uu
)

I KNOW there's a better way to do a 2-dimensional filter, poss. using findstr, but I'm still working on it. This method is very inefficient, but it seems to work on my test setup. Obvious bug is if your data had >xxx< as first element on the line: my batch would pull it as a Tag (delims="<>"). "You know the drill!". This "did the dirt" on a limited test...


Report •
Related Solutions


Ask Question