help - replace multiple strings from a file in another file

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
paul_costas
Posts: 4
Joined: 05 Aug 2010 11:34

help - replace multiple strings from a file in another file

#1 Post by paul_costas » 05 Aug 2010 11:52

Hello there
the issue is find and replace multiple strings from a file, into another file like this
string.txt contains :

Daniel Christiansen
Paul Tom
Dana Christine
and so on...

destin.txt contain large text files, stories for kids
what i want is to replace every letter or word from string.txt with it's equivalent Daniel=Christiansen and so on in the destin.txt file

i have a piece of code from this site i think:

@echo off
SETLOCAL ENABLEEXTENSIONS
SETLOCAL DISABLEDELAYEDEXPANSION
SET ELS_DRIVE=C:\XCOPY
SET test1=%ELS_DRIVE%\TEST10
SET FILE_PREFIX=destin
SET FILE_SUFFIX=.txt
echo %ELS_DRIVE%
CD %ELS_DRIVE%
cd %test1%
SET OldStr=Daniel
SET NewStr=Chrisiansen
echo %FILE_PREFIX%*%FILE_SUFFIX%
for %%i in (%FILE_PREFIX%*%FILE_SUFFIX%) do CALL :MIO %%i

:MIO
SET myTempFile=%1
IF "%myTempFile%"=="" GOTO:EOF
echo.
echo %myTempFile%
for /f "tokens=1,2 delims=. " %%a in ("%myTempFile%") do set myNameFile=%%a&set

myExtensionFile=%%b
set myFile=%myNameFile%.%myExtensionFile%.orig
ren %myTempFile% %myFile%

if "%OldStr%"=="" findstr "^::" "%~f0"&GOTO EOF
for /f "tokens=1,* delims==" %%A in ('"type %myFile%"') do (
set "line=%%A"
if defined line (
call set "line=echo.%%line:%OldStr%=%NewStr%%%"
for /f "delims=" %%X in ('"echo."%%line%%""') do %%~X>>%myTempFile%
) ELSE echo.
)

any ideea how is to be done?
Thanks

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

Re: help - replace multiple strings from a file in another f

#2 Post by SenHu » 05 Aug 2010 15:03

Code: Select all

# Script ReplaceWords.txt
var string list, line, word1, word2, content
cat "C:/test/destin.txt" > $content ; cat "C:/test/string.txt" > $list
lex "1" $list > $line
while ($line <> "")
do
    wex "1" $line > $word1 ; wex "1" $line > $word2
echo -e "DEBUG: Replacing " $word1 " with " $word2
    while ( { sen -c ("^"+$word1+"^") $content } > 0 )
        sal -c ("^"+$word1+"^") $word2 $content > null
    done
    lex "1" $list > $line
done
echo $content > "C:/test/destin2.txt"


Script is in biterscripting. Save it in file "C:/Scripts/ReplaceWords.txt", run it by entering the command.

Code: Select all

script "C:/Scripts/ReplaceWords.txt"


I have tested this script.

paul_costas
Posts: 4
Joined: 05 Aug 2010 11:34

Re: help - replace multiple strings from a file in another f

#3 Post by paul_costas » 05 Aug 2010 15:10

SenHu wrote:

Code: Select all

# Script ReplaceWords.txt
var string list, line, word1, word2, content
cat "C:/test/destin.txt" > $content ; cat "C:/test/string.txt" > $list
lex "1" $list > $line
while ($line <> "")
do
    wex "1" $line > $word1 ; wex "1" $line > $word2
echo -e "DEBUG: Replacing " $word1 " with " $word2
    while ( { sen -c ("^"+$word1+"^") $content } > 0 )
        sal -c ("^"+$word1+"^") $word2 $content > null
    done
    lex "1" $list > $line
done
echo $content > "C:/test/destin2.txt"


Script is in biterscripting. Save it in file "C:/Scripts/ReplaceWords.txt", run it by entering the command.

Code: Select all

script "C:/Scripts/ReplaceWords.txt"


I have tested this script.


Thank you very much!!!
what i was interested and i forgot to mention is that i was need it without third part..
is that possible?
somewhere i made a mistake in syntax and i cannot solve it
Thanks again!

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

Re: help - replace multiple strings from a file in another f

#4 Post by aGerman » 05 Aug 2010 16:01

Probably this can be done using native batch, but it's slow! If you have big files it will run a very long time.
I tried:
string.txt

Code: Select all

Daniel Christiansen
Paul Tom
Dana Christine



source.txt

Code: Select all

<Daniel said:>

"I saw Paul today. He asked for you, Dana."



Replace.bat

Code: Select all

@echo off &setlocal

:processFile
>"destin.txt" type nul
for /f "delims=: tokens=1*" %%a in ('findstr /n "^" "source.txt"') do (
  set "line=%%b"
  call :processLine
)
goto :eof

:processLine
if not defined line (
  >>"destin.txt" echo.
  goto :eof
)
for /f "usebackq tokens=1* delims= " %%a in ("string.txt") do (
  call set "line=%%line:%%a=%%b%%"
)
set "line=%line:^=^^%"
set "line=%line:&=^&%"
set "line=%line:<=^<%"
set "line=%line:>=^>%"
set "line=%line:|=^|%"
>>"destin.txt" echo.%line%
goto :eof



Result in destin.txt

Code: Select all

<Christiansen said:>

"I saw Tom today. He asked for you, Christine."


Regards
aGerman

paul_costas
Posts: 4
Joined: 05 Aug 2010 11:34

Re: help - replace multiple strings from a file in another f

#5 Post by paul_costas » 05 Aug 2010 16:25

aGerman wrote:Probably this can be done using native batch, but it's slow! If you have big files it will run a very long time.
I tried:
string.txt

Code: Select all

Daniel Christiansen
Paul Tom
Dana Christine



source.txt

Code: Select all

<Daniel said:>

"I saw Paul today. He asked for you, Dana."



Replace.bat

Code: Select all

@echo off &setlocal

:processFile
>"destin.txt" type nul
for /f "delims=: tokens=1*" %%a in ('findstr /n "^" "source.txt"') do (
  set "line=%%b"
  call :processLine
)
goto :eof

:processLine
if not defined line (
  >>"destin.txt" echo.
  goto :eof
)
for /f "usebackq tokens=1* delims= " %%a in ("string.txt") do (
  call set "line=%%line:%%a=%%b%%"
)
set "line=%line:^=^^%"
set "line=%line:&=^&%"
set "line=%line:<=^<%"
set "line=%line:>=^>%"
set "line=%line:|=^|%"
>>"destin.txt" echo.%line%
goto :eof



Result in destin.txt

Code: Select all

<Christiansen said:>

"I saw Tom today. He asked for you, Christine."


Regards
aGerman


Thanks aGerman, i have test it and it works just fine. When it encounters char "*" it says: =Paul%" is unexpected at this time. I dont have only Caps letters, i have numbers and allmost all the other characters.
Where should i modify?
Thanks again!

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

Re: help - replace multiple strings from a file in another f

#6 Post by aGerman » 05 Aug 2010 17:01

You can't use asterisks. This is one of the special characters (wildcards) in batch.

Here I attached a small VBScript. Save the code as Xchange.vbs.


Call it from your batch file:

Code: Select all

@echo off &setlocal
for /f "usebackq tokens=1* delims= " %%a in ("string.txt") do (
  call Xchange.vbs "source.txt" -l "%%a" -l "%%b"
)


This will replace directly in source.txt

Regards
aGerman

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

Re: help - replace multiple strings from a file in another f

#7 Post by ghostmachine4 » 05 Aug 2010 18:58

aGerman wrote:You can't use asterisks. This is one of the special characters (wildcards) in batch.

Here I attached a small VBScript. Save the code as Xchange.vbs.


Call it from your batch file:

Code: Select all

@echo off &setlocal
for /f "usebackq tokens=1* delims= " %%a in ("string.txt") do (
  call Xchange.vbs "source.txt" -l "%%a" -l "%%b"
)


This will replace directly in source.txt

Regards
aGerman


well, it vbscript is used, why not do everything with vbscript? In the above the batch is calling the script for every line encountered in string.txt, which will be slow. Doing everything inside vbscript, you only need to call cscript once.

Code: Select all

Set objFS = CreateObject("Scripting.FileSystemObject")
Set d = CreateObject("Scripting.Dictionary")
Set objArgs = WScript.Arguments
strFile=objArgs(0)
strBigFile=objArgs(1)
Set objFile = objFS.OpenTextFile(strFile)
Do Until objFile.AtEndOfLine
   strLine=objFile.ReadLine
   s = Split(strLine," ")
   If Not d.Exists(s(0)) Then
      d.Add s(0), s(1)
   End If
Loop
y=d.Keys
objFile.Close
Set objFile = objFS.OpenTextFile(strBigFile)
Do Until objFile.AtEndOfStream
   strLine=objFile.ReadLine
   For Each strkey In y      
      strLine = Replace( strLine, strkey,  d.Item(strkey) )
   Next
   WScript.Echo strLine
Loop
objFile.Close


Code: Select all

C:\test>cscript //nologo test.vbs string.txt bigfile.txt
<Christiansen said:>

"I saw Tom today. He asked for you, Christine."

miskox
Posts: 356
Joined: 28 Jun 2010 03:46

Re: help - replace multiple strings from a file in another f

#8 Post by miskox » 06 Aug 2010 12:17

Though I prefer native batch (to be completely independent) sometimes it just can't handle complex strings. I use gsar (just google for it). You can replace *every* character using HEX/DEC codes.

Saso

paul_costas
Posts: 4
Joined: 05 Aug 2010 11:34

Re: help - replace multiple strings from a file in another f

#9 Post by paul_costas » 06 Aug 2010 17:59

aGerman wrote:You can't use asterisks. This is one of the special characters (wildcards) in batch.

Here I attached a small VBScript. Save the code as Xchange.vbs.


Call it from your batch file:

Code: Select all

@echo off &setlocal
for /f "usebackq tokens=1* delims= " %%a in ("string.txt") do (
  call Xchange.vbs "source.txt" -l "%%a" -l "%%b"
)


This will replace directly in source.txt

Regards
aGerman

************
hi again
i have checked with all characters to replace and this is what i found
the program takes first word and replaces every where, which is not god for me
what i need is, the program to check as follows
if first row is 001 *001* in string.txt, and the second row in string.txt is 010 *010* and so on.., the batch to see if after 001 comes a 010, because, if we have the next phrase :
111 001 001 100 110 011 001 100 011 100 000 111 000 011 001 10 (spaces between are to ignore..are for the reader to see) he is doing like this
111 = x, 001 = g, 001 = g , "1" stays one!!!!!, again 001 = g
so, he search after 001 = g, and replaces all, and then the rest. Could it be possible to see if the next 3 letters from destination text are a match from string.txt???. If i was not crystal clear, i will try my best to explain again

thx

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

Re: help - replace multiple strings from a file in another f

#10 Post by aGerman » 07 Aug 2010 07:30

Hmm. That means the lines in your file are like that:
11100100110011001100110001110000011100001100110
How should a batch (or VBScript) know which digits belong together and which not?
You have to define it absolutely clear.
e.g.
-take the line
-split the line to parts of 3 (?) digits, starting on position 0 (?)
-define what happens if the last part is shorter than 3 digits
-match each part if it can be found in string.txt
-...

Regards
aGerman

Farlane
Posts: 1
Joined: 25 Feb 2020 05:35

Re: help - replace multiple strings from a file in another file

#11 Post by Farlane » 25 Feb 2020 06:29

Hello,
My name is farlane and I'm french, sorry if my english is not very good ...
I'm relaunching this subject because I am currently facing the same problem.
But my "source.txt" and "string.txt" files have a format that is very different from the original subject.

This is my "source.txt" file:

Code: Select all

@echo off
@REM Traitement   
@REM if processing in error   
@REM if not %h_status%==E goto FIN   

@REM si transfert sortant alors exit   
@REM if not %g_togate%==O goto FIN   

if '%arc_tip1%' == '' goto FIN   
if '%arc_tip1:~0,5%' == 'PERISE-' goto PERISE   
move %Y_dossier%\%arbo_sit% %Y_dossier%\DONNEES\%arc_tip1%   
:PERISE   
move %Y_dossier%\%arbo_sit% D:\PERISE_CARTO\DONNEES\%arc_tip1%_%TIP_ident%   

:FIN   
EXIT 
And this is my "string.txt" file:

Code: Select all

%Y_dossier% &ARBO
%arc_tip1% &RENOM
%g_togate% &STATUT
%arbo_sit% &LOCAL
My problem mainly comes from the characters "%" and "&" which I have to replace in the script...
Does anyone know how to do this in batch script ?
Thank you in advance for your help :wink:

jeb
Expert
Posts: 967
Joined: 30 Aug 2007 08:05
Location: Germany, Bochum

Re: help - replace multiple strings from a file in another file

#12 Post by jeb » 25 Feb 2020 07:41

Without seeing the relevant part of your replace code it's hard to say ....

There shouldn't be any problems with delayed expansion.

Code: Select all

set "line=!line:%%Y_dossier%%=%%Z_dossier%%!"
set "line=!line:&=...et...!"
echo(!line!

Post Reply