change a single character in a file

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
flo_vie
Posts: 1
Joined: 06 Mar 2009 10:17

change a single character in a file

#1 Post by flo_vie » 06 Mar 2009 10:23

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!

avery_larry
Expert
Posts: 391
Joined: 19 Mar 2009 08:47
Location: Iowa

#2 Post by avery_larry » 19 Mar 2009 10:23

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

SenHu
Posts: 19
Joined: 19 Mar 2009 14:57

Use biterscripting for automated file manipulation

#3 Post by SenHu » 19 Mar 2009 15:25

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.

    1. Download and install biterscripting from http://www.biterscripting.com .
    2. Install all sample scripts using the following command

    Code: Select all

    script "http://www.biterscripting.com/Download/SS_AllSamples.txt"
This entire installation usually takes only 3-4 minutes.

Sen

Post Reply