Solved Find and replace characters in txt file using batch file

April 29, 2013 at 15:06:24
Specs: Windows 7, 4
For example file name "EDG091_1.txt" will contain the following:

DECLARE FIG$NEWGENERATOR(*)BYTE DATA(
022H, 044H, 043H, 003H, 003H, 007H, 005H, 003H, 053H, 006H
006H, 023H, 003H, 044H, 011H, 073H, 003H, 022H, 055H, 074H
004H, 003H, 123H, 045H, 004H, 084H);
DECLARE FIG$OLDMACHINE(*)BYTE DATA(
022H, 044H, 063H, 003H, 003H, 007H, 085H, 003H, 063H, 006H
006H, 023H, 004H, 044H, 011H, 033H, 003H, 022H, 055H, 044H
004H, 003H, 123H, 045H, 004H, 024H);

I am trying to find all the "003H" entries associated with the title "DECLARE FIG$NEWGENERATOR(*)BYTE DATA" and replace them with "001H"...Then find all the "044H" entries associated with the title "DECLARE FIG$OLDMACHINE(*)BYTE DATA" and replace all those with "014H"

Lastly to output a new file called EDG091_A.txt

Thanks in advance!!


See More: Find and replace characters in txt file using batch file

Report •

✔ Best Answer
May 14, 2013 at 07:25:12
:: =====  script starts here  ===============
:: lesson learned: set/a X1=X+1 does NOT need !!
:: p71c.bat  2013-05-10  3:45:24.65
@echo off > NEWFILE & setLocal enableDELAYedeXpansioN

set N=
for /f "tokens=* " %%a in (tabfile) do (
set/a N+=1
  if "%%a"=="DECLARE FIG$NEWGENERATOR(*)BYTE DATA(" set/a X=!N!
  if "%%a"=="DECLARE FIG$OLDMACHINE(*)BYTE DATA("   set/a Y=!N!
)
set/a X1=X+1 & set/a X2=X+2 & set/a X3=X+3
set/a Y1=Y+1 & set/a Y2=Y+2 & set/a Y3=Y+3

:sub1
set N=
for /f "tokens=* delims= " %%a in (tabfile) do (
set/a N+=1
set S=%%a
  for %%i in (!X1! !X2! !X3!) do (if %%i==!N! set S=!S:003H=001H!)
  for %%i in (!Y1! !Y2! !Y3!) do (if %%i==!N! set S=!S:044H=014H!)
echo.!S!
)>>  NEWFILE
goto :eof
::======  script ends here  =================

=====================
M2 Golden-Triangle



#1
April 29, 2013 at 16:14:39
tested briefly

:: =====  script starts here  ===============
:: 
:: oldnew.bat  2013-04-30  5:43:49.48
@echo off > NEWFILE & setLocal enableDELAYedeXpansioN

set B=
for /f "tokens=* delims= " %%a in (myfile) do (
set S=%%a
  if defined B (
    set S=!S:044H=014H!
  ) else (
    set S=!S:003H=001H!
  )

  if "!S!" equ "DECLARE FIG$OLDMACHINE(*)BYTE DATA(" set B=B
echo.!S!
)>> NEWFILE
::======  script ends here  =================

=====================
M2 Golden-Triangle


Report •

#2
April 30, 2013 at 07:08:36
Thanks for the quick reply. This worked, but will only work if the starting entry is always "DECLARE FIG$NEWGENERATOR(*)BYTE DATA(". What if I had a different starting entry like below and still trying to make the same changes:

DECLARE LIL$GENERATOR(*)BYTE DATA(
022H, 044H, 043H, 003H, 003H, 007H, 005H, 003H, 053H, 006H
006H, 023H, 003H, 044H, 011H, 073H, 003H, 022H, 055H, 074H
004H, 003H, 123H, 045H, 004H, 084H);
DECLARE FIG$NEWGENERATOR(*)BYTE DATA(
022H, 044H, 043H, 003H, 003H, 007H, 005H, 003H, 053H, 006H
006H, 023H, 003H, 044H, 011H, 073H, 003H, 022H, 055H, 074H
004H, 003H, 123H, 045H, 004H, 084H);
DECLARE FIG$OLDMACHINE(*)BYTE DATA(
022H, 044H, 063H, 003H, 003H, 007H, 085H, 003H, 063H, 006H
006H, 023H, 004H, 044H, 011H, 033H, 003H, 022H, 055H, 044H
004H, 003H, 123H, 045H, 004H, 024H);


I am trying to find all the "003H" entries associated with the title "DECLARE FIG$NEWGENERATOR(*)BYTE DATA" and replace them with "001H"...Then find all the "044H" entries associated with the title "DECLARE FIG$OLDMACHINE(*)BYTE DATA" and replace all those with "014H"


Report •

#3
April 30, 2013 at 07:32:56
Too late today. I'll hit it in the morning. That'll give you time for any more revisions.

=====================
M2 Golden-Triangle


Report •

Related Solutions

#4
April 30, 2013 at 07:45:54
Yea sure no problem, appreciate the help!

Report •

#5
May 2, 2013 at 10:12:06
I've played around with the code a little bit, still wasnt able to get it. Limited experience with batch file syntax.

Report •

#6
May 3, 2013 at 00:17:31
Post the file and the requirement.

=====================
M2 Golden-Triangle


Report •

#7
May 3, 2013 at 08:11:56
DECLARE LIL$GENERATOR(*)BYTE DATA(
022H, 044H, 043H, 003H, 003H, 007H, 005H, 003H, 053H, 006H
006H, 023H, 003H, 044H, 011H, 073H, 003H, 022H, 055H, 074H
004H, 003H, 123H, 045H, 004H, 084H);
DECLARE FIG$NEWGENERATOR(*)BYTE DATA(
022H, 044H, 043H, 003H, 003H, 007H, 005H, 003H, 053H, 006H
006H, 023H, 003H, 044H, 011H, 073H, 003H, 022H, 055H, 074H
004H, 003H, 123H, 045H, 004H, 084H);
DECLARE FIG$OLDMACHINE(*)BYTE DATA(
022H, 044H, 063H, 003H, 003H, 007H, 085H, 003H, 063H, 006H
006H, 023H, 004H, 044H, 011H, 033H, 003H, 022H, 055H, 044H
004H, 003H, 123H, 045H, 004H, 024H);
DECLARE HIG$MACHINE(*)BYTE DATA(
021H, 044H, 062H, 003H, 033H, 007H, 085H, 003H, 055H, 006H
006H, 023H, 004H, 048H, 011H, 033H, 045H, 022H, 055H, 064H
004H, 003H, 123H, 044H, 004H, 027H);


I am trying to find all the "003H" entries associated with the title "DECLARE FIG$NEWGENERATOR(*)BYTE DATA" and replace them with "001H"...Then find all the "044H" entries associated with the title "DECLARE FIG$OLDMACHINE(*)BYTE DATA" and replace all those with "014H"


Report •

#8
May 3, 2013 at 18:57:47
This might work, but I have a feeling it has more holes than those (4000) counted in "Blackburn Lancashire":
::===== begin batch
@echo off>p71.out & setlocal enabledelayedexpansion
set declare fig$newgenerator=003H
set declare fig$newgenerator1=001H
set declare fig$oldmachine=044H
set declare fig$oldmachine1=041H
for /f "tokens=1* delims=(" %%a in (p71) do (
set test=!%%a!
if defined test (
>>p71.out echo %%a(%%b
set xx=!test!
set repl=!%%a1!
) else (
call :replace "%%a"
)
)
goto :eof

:replace
set z=%1
if not defined xx goto :eko
set z=!z:%xx%=%repl%!
:eko
>> p71.out echo %z:~1,-1%
::==== end batch
"now we know how many holes it takes to fill the Albert Hall!"


Report •

#9
May 3, 2013 at 21:13:33
Would you consider using a more powerful language?

#!/usr/bin/perl

while (<>) {
    if (/DECLARE FIG\$NEWGENERATOR/ .. /;/) {s/003H/001H/g;}
    if (/DECLARE FIG\$OLDMACHINE/ .. /;/) {s/044H/014H/g;}
    print;
}

Executed as:
D:\test>fishmonger.pl < EDG091_1.txt > EDG091_A.txt


Report •

#10
May 4, 2013 at 22:48:49
:: =====  script starts here  ===============
@echo off > NEWFILE & setLocal enableDELAYedeXpansioN

for /f "tokens=* delims= " %%a in (myfile) do (
set/a N+=1
set S=%%a
    if !B!==A for %%i in (1 2 3) do (if %%i==!N! set S=!S:003H=001H!)
    if !B!==B for %%i in (1 2 3) do (if %%i==!N! set S=!S:044H=014H!)
  if "%%a" equ "DECLARE FIG$NEWGENERATOR(*)BYTE DATA(" set B=A&& set N=
  if "%%a" equ "DECLARE FIG$OLDMACHINE(*)BYTE DATA("   set B=B&& set N=
echo.!S!
)>>  NEWFILE
::======  script ends here  =================

=====================
M2 Golden-Triangle


Report •

#11
May 9, 2013 at 12:05:31
@Mechanix2Go
Thanks that code worked! Now I'm running into another issue where the titles are "tabbed" therefore the "if" comparison is not working. Titles are a "single tab", and hex values are "double tab". Example of what I'm talking about (the word "tab" isn't in actual file, but when posting everything gets "left justified" so i had to put "tab" to represent the [tab space]) :

tab DECLARE LIL$GENERATOR(*)BYTE DATA(
tab tab 022H, 044H, 043H, 003H, 003H, 007H, 005H, 003H, 053H, 006H
tab tab 006H, 023H, 003H, 044H, 011H, 073H, 003H, 022H, 055H, 074H
tab tab 004H, 003H, 123H, 045H, 004H, 084H);
tab DECLARE FIG$NEWGENERATOR(*)BYTE DATA(
tab tab 022H, 044H, 043H, 003H, 003H, 007H, 005H, 003H, 053H, 006H
tab tab 006H, 023H, 003H, 044H, 011H, 073H, 003H, 022H, 055H, 074H
tab tab 004H, 003H, 123H, 045H, 004H, 084H);
tab DECLARE FIG$OLDMACHINE(*)BYTE DATA(
tab tab 022H, 044H, 063H, 003H, 003H, 007H, 085H, 003H, 063H, 006H
tab tab 006H, 023H, 004H, 044H, 011H, 033H, 003H, 022H, 055H, 044H
tab tab 004H, 003H, 123H, 045H, 004H, 024H);
tab DECLARE HIG$MACHINE(*)BYTE DATA(
tab tab 021H, 044H, 062H, 003H, 033H, 007H, 085H, 003H, 055H, 006H
tab tab 006H, 023H, 004H, 048H, 011H, 033H, 045H, 022H, 055H, 064H
tab tab 004H, 003H, 123H, 044H, 004H, 027H);

Also I will like to print the new file with appropriate tabs. I apologize for the constant change in requirements, over simplified the problem to begin with.


Report •

#12
May 9, 2013 at 13:51:52
Do you need the tabs in the output?

=====================
M2 Golden-Triangle


Report •

#13
May 9, 2013 at 13:56:35
yes, i need "tabs" in the output. Not the word but the actual function just to clarify.

Report •

#14
May 10, 2013 at 00:57:00
I need the FILE. You can zip it and email it.

=====================
M2 Golden-Triangle


Report •

#15
May 10, 2013 at 06:48:00
whats the address?? Or how do I send you a direct email on here??

Report •

#16
May 10, 2013 at 07:34:26
sent via PM

attach a ZIP so it doesn't get mangled

=====================
M2 Golden-Triangle


Report •

#17
May 14, 2013 at 07:25:12
✔ Best Answer
:: =====  script starts here  ===============
:: lesson learned: set/a X1=X+1 does NOT need !!
:: p71c.bat  2013-05-10  3:45:24.65
@echo off > NEWFILE & setLocal enableDELAYedeXpansioN

set N=
for /f "tokens=* " %%a in (tabfile) do (
set/a N+=1
  if "%%a"=="DECLARE FIG$NEWGENERATOR(*)BYTE DATA(" set/a X=!N!
  if "%%a"=="DECLARE FIG$OLDMACHINE(*)BYTE DATA("   set/a Y=!N!
)
set/a X1=X+1 & set/a X2=X+2 & set/a X3=X+3
set/a Y1=Y+1 & set/a Y2=Y+2 & set/a Y3=Y+3

:sub1
set N=
for /f "tokens=* delims= " %%a in (tabfile) do (
set/a N+=1
set S=%%a
  for %%i in (!X1! !X2! !X3!) do (if %%i==!N! set S=!S:003H=001H!)
  for %%i in (!Y1! !Y2! !Y3!) do (if %%i==!N! set S=!S:044H=014H!)
echo.!S!
)>>  NEWFILE
goto :eof
::======  script ends here  =================

=====================
M2 Golden-Triangle


Report •

#18
May 16, 2013 at 11:03:23
Perfect!! Thanks a lot for the help.

Report •

Ask Question