Page 1 of 1

Batch to find and replace

Posted: 29 Jul 2010 22:45
by cdeb77
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

Re: Batch to find and replace

Posted: 30 Jul 2010 04:33
by aGerman
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

Re: Batch to find and replace

Posted: 30 Jul 2010 07:22
by ghostmachine4
if you can download stuff, you can use GNU sed
example

Code: Select all

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

Re: Batch to find and replace

Posted: 30 Jul 2010 09:18
by cdeb77
Thanks you two for your replies. The vbs did a great work!
Bests regards

Re: Batch to find and replace

Posted: 30 Jul 2010 10:46
by cdeb77
Thanks you two for your replies. The vbs did a great work!
Bests regards