Batch script to find and replace

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
navanees
Posts: 11
Joined: 20 Jul 2016 16:31

Batch script to find and replace

#1 Post by navanees » 20 Jul 2016 16:47

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

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Batch script to find and replace

#2 Post by foxidrive » 20 Jul 2016 18:03

This should modify the same file in the way you want:

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

navanees
Posts: 11
Joined: 20 Jul 2016 16:31

Re: Batch script to find and replace

#3 Post by navanees » 21 Jul 2016 12:02

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

Squashman
Expert
Posts: 4486
Joined: 23 Dec 2011 13:59

Re: Batch script to find and replace

#4 Post by Squashman » 21 Jul 2016 12:09

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

navanees
Posts: 11
Joined: 20 Jul 2016 16:31

Re: Batch script to find and replace

#5 Post by navanees » 21 Jul 2016 12:44

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

Squashman
Expert
Posts: 4486
Joined: 23 Dec 2011 13:59

Re: Batch script to find and replace

#6 Post by Squashman » 21 Jul 2016 12:50

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

navanees
Posts: 11
Joined: 20 Jul 2016 16:31

Re: Batch script to find and replace

#7 Post by navanees » 21 Jul 2016 13:13

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

Squashman
Expert
Posts: 4486
Joined: 23 Dec 2011 13:59

Re: Batch script to find and replace

#8 Post by Squashman » 21 Jul 2016 13:22

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

navanees
Posts: 11
Joined: 20 Jul 2016 16:31

Re: Batch script to find and replace

#9 Post by navanees » 21 Jul 2016 13:26

Completely understand! :D

Is there a way around for this requirement?

Squashman
Expert
Posts: 4486
Joined: 23 Dec 2011 13:59

Re: Batch script to find and replace

#10 Post by Squashman » 21 Jul 2016 13:40

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

navanees
Posts: 11
Joined: 20 Jul 2016 16:31

Re: Batch script to find and replace

#11 Post by navanees » 21 Jul 2016 14:22

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

Post Reply