Batch to find and replace

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
cdeb77
Posts: 3
Joined: 29 Jul 2010 22:42

Batch to find and replace

#1 Post by cdeb77 » 29 Jul 2010 22:45

Hi all,

I need to replace all occurrences of a specific string with a blank space. More precisely, I need a batch file to find all "NaN" in a text file, and replace it with " " (a blank space). How can I do this?

Bests

CB

aGerman
Expert
Posts: 4654
Joined: 22 Jan 2010 18:01
Location: Germany

Re: Batch to find and replace

#2 Post by aGerman » 30 Jul 2010 04:33

Try

Code: Select all

set string=MyNaNString
set "string=%string:NaN= %"
echo %string%


Native Batch could replace it in a file using a FOR loop. You have to process line by line, replace all found NaN's by spaces and write it to a new file. You will find some examples if you use the search function of this forum.
But using native batch has some disadvantages:
- it's verry slow
- processing of empty lines is tricky
- you have to escape all found special characters (^<>|&) before you can write the new line
- replacing by batch is not case sesitive (so nAn or nan are replaced too)

I suggest to use a small tool in VBS that I posted here. Save the code as "Xchange.vbs"
Now you can call this tool in a batch file:
xchange.vbs "your.txt" -l "NaN" -l " "

Regards
aGerman

ghostmachine4
Posts: 319
Joined: 12 May 2006 01:13

Re: Batch to find and replace

#3 Post by ghostmachine4 » 30 Jul 2010 07:22

if you can download stuff, you can use GNU sed
example

Code: Select all

    sed -i.bak "s/NaN/ /g" file

cdeb77
Posts: 3
Joined: 29 Jul 2010 22:42

Re: Batch to find and replace

#4 Post by cdeb77 » 30 Jul 2010 09:18

Thanks you two for your replies. The vbs did a great work!
Bests regards

cdeb77
Posts: 3
Joined: 29 Jul 2010 22:42

Re: Batch to find and replace

#5 Post by cdeb77 » 30 Jul 2010 10:46

Thanks you two for your replies. The vbs did a great work!
Bests regards

Post Reply