Computing.Net > Forums > Programming > find and replace string in a file

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

find and replace string in a file

Reply to Message Icon

Name: Jaya
Date: September 16, 2009 at 08:37:47 Pacific
OS: Windows XP
Subcategory: Batch
Comment:

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.



Sponsored Link
Ads by Google

Response Number 1
Name: Mechanix2Go
Date: September 16, 2009 at 08:50:10 Pacific
Reply:

@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


0

Response Number 2
Name: Jaya
Date: September 16, 2009 at 09:23:34 Pacific
Reply:

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?


0

Response Number 3
Name: Mechanix2Go
Date: September 16, 2009 at 10:04:29 Pacific
Reply:

The code I posted changes Huey to Dewey.


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

M2


0

Response Number 4
Name: ghostdog
Date: September 17, 2009 at 05:04:54 Pacific
Reply:

you have provided your example input string, but what kind of output are you looking for? show them.

GNU win32 packages | Gawk


0

Response Number 5
Name: Jaya
Date: September 17, 2009 at 08:57:15 Pacific
Reply:

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...


0

Related Posts

See More



Response Number 6
Name: ricardo647
Date: September 17, 2009 at 11:54:33 Pacific
Reply:

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


0

Response Number 7
Name: Jaya
Date: September 17, 2009 at 13:22:52 Pacific
Reply:

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!!


0

Sponsored Link
Ads by Google
Reply to Message Icon





Use following form to reply to current message:

Login or Register to Reply
LoginRegister


Sponsored links

Ads by Google


Results for: find and replace string in a file

Find and Replace text in Batch File www.computing.net/answers/programming/find-and-replace-text-in-batch-file/12413.html

Find and replace a string in a file www.computing.net/answers/programming/find-and-replace-a-string-in-a-file/19034.html

nt batch to replace string in srt file www.computing.net/answers/programming/nt-batch-to-replace-string-in-srt-file/19260.html