Script to find and replace a string in a text file

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

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

Script to find and replace a string in a text file

#1 Post by navanees » 21 Jul 2016 17:55

I am looking for a batch script that can find and replace a string in a text file and send it to another file . Its just like the find and replace function in text or excel
The order should remain the same. Does anyone has a batch script built for this?

Here is the sample file

I have these random words and the string "water" should be replaced with "air"

Input file
========
water blubber water cloud moon water water school network hammer walking violently mediocre literature
water two window cords water zebra internet water
xylophone water home dog water ink water fun water banana uncle water mega water awesome attach water

Output file:
==========
air blubber air cloud moon air air school network hammer walking violently mediocre literature
air two window cords air zebra internet air
xylophone air home dog air ink air fun air banana uncle air mega air awesome attach air


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

Re: Script to find and replace a string in a text file

#3 Post by foxidrive » 25 Jul 2016 17:35

For a robust solution there is findrepl and jrepl.

Code: Select all

call findrepl "water" "air" <"yourfile.txt" >"your new file.txt"


Code: Select all

call jrepl "water" "air" /L  /f "yourfile.txt" /o -


The lower code changes your original file with the altered text.



findrepl
viewtopic.php?f=3&t=4697

jrepl
viewtopic.php?f=3&t=6044

Post Reply