Help with a Find and Replace script

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
amosgreg
Posts: 4
Joined: 01 Nov 2016 07:07

Help with a Find and Replace script

#1 Post by amosgreg » 01 Nov 2016 07:31

Search using terms - replace string find file - was not successful as to many common words but this is exactly what I am needing.

Task, I need to search and replace in a file (2 actually) using a series of values that I will have in a delimited file stored as Old,New format.
I have this script that will read a file, parse and return the old and new values as variables.

Code: Select all

@Echo Off
setlocal enabledelayedexpansion
For /f "tokens=1,2* delims=,;" %%a In (FromTo.txt) Do (
Echo #1Old: %%a
Echo #2New: %%b
   )
endlocal

Input

Code: Select all

xxxxx,yyyyy
aaaa,bbbb


Ouput shown below

Code: Select all

#1Old: xxxxx
#2New: yyyyy
#1Old: aaaa
#2New: bbbb

What I now need is a nested call to read another file (or 2 but I have no issues running another script for the second file. If both can be included all the better) and use the 2 variables as Search/Replace values.

I spend most of yesterday searching and in quite a few instances I see hardcoded values of the S/R terms but was unsuccessful with this scenario of passing variables in.
I also do now know what else to add to my search terms to be more specific as they describe what I need to do.

Thanks in advance for any and all help
Greg

dbenham
Expert
Posts: 2461
Joined: 12 Feb 2011 21:02
Location: United States (east coast)

Re: Help with a Find and Replace script

#2 Post by dbenham » 01 Nov 2016 08:29

Batch is generally a poor language choice for manipulating text files. Simple solutions have many limitations that frequently make them useless for the task at hand. Robust solutions require a lot of arcane code.

I have a simple solution for you that uses JREPL.BAT - a regular expression text processing utility. JREPL is pure script (hybrid batch/JScript) that runs natively on any Windows machine from XP onward - no 3rd party exe file required.

My JREPL solution uses three steps, described in comments within the code:

Code: Select all

@echo off

:: Split FromTo.txt into two temp files - From.txt and To.txt
call jrepl  "(.*?),(.*)" "$txt=$1;stdout.writeLine($2)" /jq /f "FromTo.txt" /o "From.txt" >"To.txt"

:: Perform the find and replace operations on File.txt
call jrepl From.txt To.txt /t FILE /l /f File.txt /o -

:: Delete the temp files
del From.txt To.txt


Dave Benham

amosgreg
Posts: 4
Joined: 01 Nov 2016 07:07

Re: Help with a Find and Replace script

#3 Post by amosgreg » 01 Nov 2016 08:42

Thanks Dave, I'll take a look.
I also might have not been clear in that my 2 updateable files are different from the FROMTO file.
That file only includes the search/replace entries that will be searched for in the other 2 files that are much larger.

Thanks
Greg

amosgreg
Posts: 4
Joined: 01 Nov 2016 07:07

Re: Help with a Find and Replace script

#4 Post by amosgreg » 01 Nov 2016 08:48

Also prohibited from downloading zip file here at the workplace.

Post Reply