1

For example I have the file sample.txt. This file contains:

1111101                       
2222203                       
3333303                       
44444A1                       
55555A1                       
66666A1 

Now, I want to replace user defined specific pattern. For example I have other file where use defines what he want to replace with. Example the file name is replace.txt. This file contains 2 Columns, first column for the pattern and the 2nd column for the text to be replace.

Example:

replace.txt

2222203         2222203ADD
55555A1         55555A1SUB

Now, when the batch file has been executed, I would like the file sample.txt to have a contents like this:

1111101                       
2222203ADD                       
3333303                       
44444A1                       
55555A1SUB                       
66666A1 

Also is it possible to have a "space" as part of the text to be replace(column 2?

2

1 Answer 1

1

You may use FindRepl.bat program that is a Batch-JScript hybrid application that perform these replacements in a very efficient way via regular expressions; it uses JScript language that is standard in all Windows versions from XP on. In the basic use of FindRepl.bat you redirect the input file to it and place two strings as parameters, a "search" string and a "replacement" string. For example:

< sample.txt FindRepl.bat "2222203" "2222203ADD"

Previous command will replace all 2222203 strings in the file by 2222203ADD. In order to perform the replacement of several strings, you may include several alternatives in both the search and replacement strings separated by a pipe character (this is called alternation), and include the /A switch to select this feature; for example:

< sample.txt FindRepl.bat "2222203|55555A1" /A "2222203ADD|55555A1SUB"

If you want to define the set of replacements in a separated file, you just need to load the strings from the file, assemble the alternations in two variables and use they in FindRepl preceded by an equal-sign to indicate that they are variables, not literal strings. If you want that the strings may have spaces, then you must use a different character to separate the search and replace parts in the file. For example, if you use a colon in replace.txt file this way:

2222203:2222203 ADD
55555A1:55555A1 SUB

Then the Batch file below solve your problem:

@echo off
setlocal EnableDelayedExpansion
set "search="
set "replace="
for /F "tokens=1,2 delims=:" %%a in (replace.txt) do (
   set "search=!search!|%%a"
   set "replace=!replace!|%%b"
)
set "search=!search:~1!"
set "replace=!replace:~1!"
< sample.txt FindRepl.bat =search /A =replace

You may download FindRepl.bat and review an explanation of its use from this site; you must place it in the same folder of previous program or, better yet, in a folder included in PATH variable.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.