Page 1 of 1
change a single character in a file
Posted: 06 Mar 2009 10:23
by flo_vie
Dear all,
I have following problem:
I have a file (data.txt) and want to replace "," with "!".
e.g. data.txt = "abc,def,ghi,jkl,mno"
and I want to change data.txt to "abc!def!ghi!jkl!mno"
Is this possible?
Thank you in advance for your help, this is highly appreciated!
Posted: 19 Mar 2009 10:23
by avery_larry
Try this -- though it could have problems with additional special characters:
Code: Select all
@echo off
for /f "delims=" %%a in (file.txt) do call :process "%%~a"
ren file.txt file.old
ren file.new file.txt
goto :eof
:process
set line=%~1
echo.%line:,=!%>>file.new
goto :eof
Use biterscripting for automated file manipulation
Posted: 19 Mar 2009 15:25
by SenHu
This can be done with biterscripting easily. biterscripting will correctly handle all special characters in the file.
I will restate your requirements.
You have a file data.txt. That has data of the following form.
abc,def,ghi,jkl,mno
You want to replace each comma (,) with exclamation mark (!). As a result, the processed file will have the following form.
abc!def!ghi!jkl!mno
This is easy. If you have biterscripting, the following commands will do exactly what you require.
Code: Select all
# Read data into a str variable.
var str data ; cat data.txt > $data
# Replace each comma (,) with exclaimation mark (!).
while ( { sen "^,^" $data } > 0)
sal "^,^" "!" $data
# Write data back to file
echo $data > data.txt
If you don't have biterscripting, you can get it free as follows.
This entire installation usually takes only 3-4 minutes.
Sen