Find a string in the text file and replace it!

A

Alan Tang

Hello:

Would you mind to tell me how can I find a text string [," ",] and
replace to [,"0",] form a command line?

Thanks!
 
J

Jerold Schulman

Hello:

Would you mind to tell me how can I find a text string [," ",] and
replace to [,"0",] form a command line?

Thanks!


Run the following batch:

@echo off
setlocal
copy "yourtextfile" "%TEMP%\yourtextfile"
del /q "yourtextfile"
for /f "Tokens=*" %%a in ('type "%TEMP%\yourtextfile"') do (
set line=%%a
call :parse
)
endlocal
exit /b 0
:parse
set work=%line:[," ",]=[,"0",]%
if "%line%" EQU "%work%" @echo %line%>>"yourtextfile"&goto :EOF
@echo %work%>>"yourtextfile"


If you text file contains command control characters, this will not work, but
using vbscript would.


Jerold Schulman
Windows: General MVP
JSI, Inc.
http://www.jsiinc.com
 
P

Paul R. Sadowski

Alan Tang said:
Hello:

Would you mind to tell me how can I find a text string [," ",] and replace
to [,"0",] form a command line?

I'd go with vbscript run with the cscript interpreter like this:
cscript replace.vbs

where replace.vbs would contain:

Const ForReading = 1, ForWriting = 2, ForAppending = 8
fname = "test.txt" ' filename of file to be worked on; use full path where
needed
Set fso = CreateObject("Scripting.FileSystemObject")
Set fs = fso.OpenTextFile(fname, ForReading)
all = fs.readall
Set regEx = New RegExp ' Create regular expression.
regEx.Pattern = "\[,"" "",\]" ' Set pattern.
regEx.Global = True
retstring = regEx.Replace(all, "[,""0"",]") ' Make replacement.
fs.close
Set fs = fso.OpenTextFile(fname, ForWriting)
fs.write retstring
fs.close
set fs = nothing
set fso = nothing

Be sure to backup the original file just in case!
 
A

Alan Tang

Hello:

Thanks for you reply, but I have find that when the string is found, it
will quit the batch!

Source file:
"1","324","2345","234532"
"2","324","2345","234532"
"3","324","2345","234532"
"4","324"," ","234532"
"5","324","2345","234532"
"6","324","2345","234532"
"7","324","2345","234532"
"8","324","2345","234532"
"9","324","2345","234532"
"10","324","2345","234532"

Batch:
@echo off
setlocal
if exist out.txt del out.txt

for /f "Tokens=*" %%a in ('type t.txt') do (
set line=%%a
call :parse
pause
)
endlocal
exit /b 0
:parse
set work=%line:," ",=,"0",%
if "%line%" EQU "%work%" echo %line%>>"out.txt"&goto :EOF
echo %work%>>out.txt

expect output file:
"1","324","2345","234532"
"2","324","2345","234532"
"3","324","2345","234532"
"4","324","0","234532"
"5","324","2345","234532"
"6","324","2345","234532"
"7","324","2345","234532"
"8","324","2345","234532"
"9","324","2345","234532"
"10","324","2345","234532"
 
A

Alan Tang

It have error!

D:\Temp\TMP>set work="4","324","0","234532"
","234532"" was unexpected at this time.
 
J

Jerold Schulman

It have error!

D:\Temp\TMP>set work="4","324","0","234532"
","234532"" was unexpected at this time.

Use the following replacetext.vbs file.

dim fso, readfile, contents, writefile, work
dim FullFileName, ParentDirName, Filename, NewFilename
Dim objArguments
Set objArguments = Wscript.Arguments
set fso = CreateObject("Scripting.FileSystemObject")
FullFileName=objArguments(0)
NewFilename=objArguments(1)
set readfile = fso.OpenTextFile(FullFileName, 1, false)
set writefile = fso.CreateTextFile(NewFileName, 2)
Do until readfile.AtEndOfStream = True
contents = readfile.ReadLine
contents = Replace(contents, objArguments(2), objArguments(3))
writefile.writeLine contents
loop
readfile.close
writefile.close


Jerold Schulman
Windows: General MVP
JSI, Inc.
http://www.jsiinc.com
 
P

Phil Robyn [MVP]

Alan said:
Hello:

Thanks for you reply, but I have find that when the string is found, it
will quit the batch!

Source file:
"1","324","2345","234532"
"2","324","2345","234532"
"3","324","2345","234532"
"4","324"," ","234532"
"5","324","2345","234532"
"6","324","2345","234532"
"7","324","2345","234532"
"8","324","2345","234532"
"9","324","2345","234532"
"10","324","2345","234532"

Batch:
@echo off
setlocal
if exist out.txt del out.txt

for /f "Tokens=*" %%a in ('type t.txt') do (
set line=%%a
call :parse
pause
)
endlocal
exit /b 0
:parse
set work=%line:," ",=,"0",%
if "%line%" EQU "%work%" echo %line%>>"out.txt"&goto :EOF
echo %work%>>out.txt

expect output file:
"1","324","2345","234532"
"2","324","2345","234532"
"3","324","2345","234532"
"4","324","0","234532"
"5","324","2345","234532"
"6","324","2345","234532"
"7","324","2345","234532"
"8","324","2345","234532"
"9","324","2345","234532"
"10","324","2345","234532"
- - - - - - - - - - begin screen capture - - - - - - - - - -
<Win2000> c:\cmd>type c:\temp\source.txt
"1","324","2345","234532"
"2","324","2345","234532"
"3","324","2345","234532"
"4","324"," ","234532"
"5","324","2345","234532"
"6","324","2345","234532"
"7","324","2345","234532"
"8","324","2345","234532"
"9","324","2345","234532"
"10","324","2345","234532"

<Win2000> c:\cmd>demo\XSource

<Win2000> c:\cmd>type c:\temp\source.txt
"1","324","2345","234532"
"2","324","2345","234532"
"3","324","2345","234532"
"4","324","0","234532"
"5","324","2345","234532"
"6","324","2345","234532"
"7","324","2345","234532"
"8","324","2345","234532"
"9","324","2345","234532"
"10","324","2345","234532"

<Win2000> c:\cmd>rlist demo\XSource.cmd
=====begin c:\cmd\demo\XSource.cmd ====================
01. @echo off
02. setlocal
03. type nul > c:\temp\source.new
04. if exist c:\temp\source.bak del c:\temp\source.bak
05. copy c:\temp\source.txt c:\temp\source.bak > nul
06. for /f "tokens=*" %%a in (c:\temp\source.txt) do call :replace %%a
07. del c:\temp\source.txt
08. move c:\temp\source.new c:\temp\source.txt
09. goto :EOF
10. :replace
11. set rec=%*
12. set rec=%rec:," ",=,"0",%
13. echo>>c:\temp\source.new %rec%
14. goto :EOF
=====end c:\cmd\demo\XSource.cmd ====================
- - - - - - - - - - end screen capture - - - - - - - - - -
 
A

Alan Tang

Hello:

Would you mind to explain the following statament? I would like to
learn more about this!

11. set rec=%*
what is meaning of %*

12. set rec=%rec:," ",=,"0",%
What is it usage? How to compare?

13. echo>>c:\temp\source.new %rec%
Why it can echo the result to the source.new file?

Thanks a lot!
 
P

Phil Robyn [MVP]

Alan said:
Hello:

Would you mind to explain the following statament? I would like to
learn more about this!

11. set rec=%*
what is meaning of %*

'%*' means 'all arguments' or 'everything that was passed as parameters'.

call :sub the quick brown fox jumps over the lazy dog
..
..
..
:sub
set rec=%*
:: rec is [the quick brown fox jumps over the lazy dog]
12. set rec=%rec:," ",=,"0",%
What is it usage? How to compare?

changes every instance of the string

," ",

in variable rec to the string

,"0",

which is what you said you wanted to do. How to compare?
What do you mean by 'How to compare'?
13. echo>>c:\temp\source.new %rec%
Why it can echo the result to the source.new file?

In the example, your original file source.txt is backed up to
source.bak, source.new is created by reading source.txt one
line at a time, replacing all instances of the string [," ",]
with the string [,"0",]* and writing each line (each 'rec')
to a new file called source.new. Afterwards, source.new
becomes source.txt, thus achieving the original purpose of
modifying source.txt.

* brackets are not part of the strings; they are used solely
for descriptive clarity here.
Thanks a lot!

You're welcome!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top