find and replace string in a file

September 16, 2009 at 08:37:47
Specs: Windows XP
Need to find and replace string in a file using Batch program. It is a text file (but with a different extension simple text and there are blanks in the file). I am a Mainframe programmer and new to Batch programming. I went through some of the sites to figure out how this could be done but in vain. I did follow the steps provided in one of the forums of this site (CHANGE.COM) but it did not work for me. Could you some one provide me a sample code which i could use for this? Would appreciate if it is available with a detailed explanation.

See More: find and replace string in a file

Report •


#1
September 16, 2009 at 08:50:10
@echo off > newfile & setLocal enableDELAYedexpansion

for /f "tokens=* delims= " %%a in (myfile) do (
set str=%%a
set str=!str:Huey=Dewey!
echo !str!>> newfile
)


=====================================
Helping others achieve escape felicity

M2


Report •

#2
September 16, 2009 at 09:23:34
Eg: My file XXX.TXT (around some 200 chars) . Has a string that looks like this:

AAAAAAA ******** AAAAAA ******** AAAA**************************** XXXXXXXXXXXXX**************** XXXXXXX **************** XXXXXXXXXXXX **************** ************************************** "

There is no change in the length of the string but lot of blanks and "*****" characters. Starting from say 50th character it needs to be changed till the 120th character.

I seriously did not understand ur example code...Possible could u please explain?


Report •

#3
September 16, 2009 at 10:04:29
The code I posted changes Huey to Dewey.


=====================================
Helping others achieve escape felicity

M2


Report •

Related Solutions

#4
September 17, 2009 at 05:04:54
you have provided your example input string, but what kind of output are you looking for? show them.

GNU win32 packages | Gawk


Report •

#5
September 17, 2009 at 08:57:15
Here is the actual one:

Input string is:
TST123 ************ THIS IS A TEST ******************** HEADER LINE START ******************* HEADER LINE 1 ********* HEADER LINE 2 ************* TRAILER LINE 1 *************** TRAILER LINE 2 *******

OUtput String should be:
TST123 ************ THIS IS A TEST ******************** CHANGE ****************************** FIRST SET OF CHG*************1234567# DONE ******** TRAILER LINE 1 *************** TRAILER LINE 2 ******

Both the strings are of the same length. But i need to change the characters starting from a particular position (XX) till a particular postion (YY). Thanks for the help...


Report •

#6
September 17, 2009 at 11:54:33
It seems to work. Test it some more.

@echo off
setlocal enabledelayedexpansion
set strg1=my string
set strg2=my other string
set file=my file.xyz
set newfile=newfile.xyz
del "%newfile%"
set pos1=24
set pos2=49
::numbers 24 and 49 are arbitrary
::count positions starting from 0
set /a range=%pos2%-%pos1%
for /f "usebackq delims=" %%a in ("!file!") do (
set line=%%a
set strline=!line:~%pos1%,%range%!
echo !strline!>"%temp%\#"
call :findx
)
goto :eof

:findx
findstr "%strg1%" "%temp%\#">nul
set change=no
if %errorlevel%==0 call :change
if %change%==no echo !line!>>"!newfile!
goto :eof

:change
set change=yes
set line=!line:%strline%=BaAtTcChHh!
set strline=!strline:%strg1%=%strg2%!
set xline=!line:BaAtTcChHh=%strline%!
echo !xline!>>"%newfile%"
goto :eof


Report •

#7
September 17, 2009 at 13:22:52
It did work. that was just wonderful. Thanks!!!

I am new to batch programming and wrote so many small codes which resulted in CRAZY outputs. PLEASE COPYRIGHT the code :-)

Thanks a ton!!


Report •

#8
December 4, 2009 at 16:07:05
I tried Both on vista (64bit) and this just outputs:
!str!

This is output like the number of lines in the old file.
Why is this?

and why is it: !str! and not: %str%


Report •


Ask Question