Batch script to find and replace
Moderator: DosItHelp
Batch script to find and replace
I have the below data in a file and wanted to write a batch to replace the data as below
Original data
==============
Name: xyz
Amount: 123
Name: abc
Amount: 456
Name: def
Amount: 789
Name: ghi
Amount: 012
Name: ijk
Amount: 345
Name: lmn
Amount: 678
Name: uvw
Amount: 901
Name: xyz
Amount: 234
Data in the output file should look something like this
=====================================
Name: xyz
Amount: 0
Name: abc
Amount: 0
Name: def
Amount: 0
Name: ghi
Amount: 0
Name: ijk
Amount: 0
Name: lmn
Amount: 0
Name: uvw
Amount: 0
Name: xyz
Amount: 0
-NN
Original data
==============
Name: xyz
Amount: 123
Name: abc
Amount: 456
Name: def
Amount: 789
Name: ghi
Amount: 012
Name: ijk
Amount: 345
Name: lmn
Amount: 678
Name: uvw
Amount: 901
Name: xyz
Amount: 234
Data in the output file should look something like this
=====================================
Name: xyz
Amount: 0
Name: abc
Amount: 0
Name: def
Amount: 0
Name: ghi
Amount: 0
Name: ijk
Amount: 0
Name: lmn
Amount: 0
Name: uvw
Amount: 0
Name: xyz
Amount: 0
-NN
Re: Batch script to find and replace
This should modify the same file in the way you want:
This uses a native Windows batch script called Jrepl.bat written by Dave Benham
viewtopic.php?f=3&t=6044
Code: Select all
call jrepl "^Amount: .*" "Amount: 0" /f "myfile.txt" /o -
This uses a native Windows batch script called Jrepl.bat written by Dave Benham
viewtopic.php?f=3&t=6044
Re: Batch script to find and replace
Thanks and appreciate your response.
The script worked well however from troubleshooting perspective it seems I will have tough time in fixing the code in case of an issue in future.
I'm sure this can also be achieved with a simple batch script using string manipulation/operation.
Appreciate if someone can help me with that
-NN
The script worked well however from troubleshooting perspective it seems I will have tough time in fixing the code in case of an issue in future.
I'm sure this can also be achieved with a simple batch script using string manipulation/operation.
Appreciate if someone can help me with that
-NN
Re: Batch script to find and replace
navanees wrote:I'm sure this can also be achieved with a simple batch script using string manipulation/operation.
Well you don't even need to do that. In its simplest form based on your input and output examples all you would need to do is this.
Code: Select all
@echo off
(FOR /F "delims=" %%G IN ('type Input.txt ^|find "Name:"') DO (
ECHO %%G
ECHO Amount: 0)
)>Output.txt
Re: Batch script to find and replace
I need to retain the lines that have Name:
instead I need to replace where it sees Amount: to >>>>> Amount: 0, regardless of what value it has
instead I need to replace where it sees Amount: to >>>>> Amount: 0, regardless of what value it has
Re: Batch script to find and replace
navanees wrote:I need to retain the lines that have Name:
instead I need to replace where it sees Amount: to >>>>> Amount: 0, regardless of what value it has
Going to assume you are replying to me. I am not understanding your response though. The output from my script matches your output example.
Code: Select all
Name: xyz
Amount: 0
Name: abc
Amount: 0
Name: def
Amount: 0
Name: ghi
Amount: 0
Name: ijk
Amount: 0
Name: lmn
Amount: 0
Name: uvw
Amount: 0
Name: xyz
Amount: 0
Re: Batch script to find and replace
Yes I was replying to you..
You are awesome, I bet! and it worked. Fault is mine as my requirement is slightly different here. (and I am not that expert in scripting and I tried to make it simple with my previous example!)
Here is what I am actually looking for
Input file
================================
Name: Mike
Age: 30
Place: Pittsuburgh
Company: xyz
amount: 2500
Name: George
amount: 110
Name: Sam
Age: 30
amount: 5000
Name: Jim
Company: abc
amount: 20000
output
=======================================
Name: Mike
Age: 30
Place: Pittsuburgh
Company: xyz
amount: 0
Name: George
amount: 0
Name: Sam
Age: 30
amount: 0
Name: Jim
Company: abc
amount: 0
=======================================
basically where ever it sees the amount: NNNNN in the file, it should replace it with amount: 0
You are awesome, I bet! and it worked. Fault is mine as my requirement is slightly different here. (and I am not that expert in scripting and I tried to make it simple with my previous example!)
Here is what I am actually looking for
Input file
================================
Name: Mike
Age: 30
Place: Pittsuburgh
Company: xyz
amount: 2500
Name: George
amount: 110
Name: Sam
Age: 30
amount: 5000
Name: Jim
Company: abc
amount: 20000
output
=======================================
Name: Mike
Age: 30
Place: Pittsuburgh
Company: xyz
amount: 0
Name: George
amount: 0
Name: Sam
Age: 30
amount: 0
Name: Jim
Company: abc
amount: 0
=======================================
basically where ever it sees the amount: NNNNN in the file, it should replace it with amount: 0
Re: Batch script to find and replace
navanees wrote:Yes I was replying to you..
You are awesome, I bet! and it worked. Fault is mine as my requirement is slightly different here. (and I am not that expert in scripting and I tried to make it simple with my previous example!)
So you learned your first lesson when asking for script help. Every detail makes a difference.
Going forward I suggest you read this thread before posting anymore questions.
viewtopic.php?f=3&t=6108
Re: Batch script to find and replace
Completely understand!
Is there a way around for this requirement?
Is there a way around for this requirement?
Re: Batch script to find and replace
navanees wrote:Is there a way around for this requirement?
Code: Select all
@echo off
setlocal enabledelayedexpansion
(FOR /F "tokens=1,2 delims=]" %%G IN ('type Input.txt ^|find /N /V ""') DO (
set line=%%H
IF /I "!line:~0,6!"=="amount" (
echo amount: 0
) else (
ECHO.%%H
)
)
)>Output.txt
Re: Batch script to find and replace
That was just AWESOME!!!!!!!!!!!!!!
I was working on this for more than a week! and I cant just explain how well it worked!
THANKS A LOT!!!!
-NN
I was working on this for more than a week! and I cant just explain how well it worked!
THANKS A LOT!!!!
-NN