Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Replace a searched line in the Text file with other string

Status
Not open for further replies.

yarondavid

Programmer
May 8, 2006
20
IL
I want to know how I can replace a searched line in the Text file with other line string.
I mean to delete a specific line that found in file and insert my string instead of that line.
 
I used this script for help but I still have a problem, the two next lines in the file are also deleted

This is my part of script
==============================
string envBaseFile, LineInfo
integer Len=0
long Pos=0

strfmt envBaseFile "%s\envBase.bat" CopyRelFolder

fopen 0 envBaseFile READWRITE
while NOT feof 0
fgets 0 LineInfo
if strfind LineInfo "Set USER_BASE="
strlen LineInfo Len
Pos = Len
fseek 0 (-Pos) 1
fdelblock 0 Len
fputs 0 "set USER_BASE=C:\TEMP\"
exitwhile
endif
endwhile

fclose 0

 
This is the source text file:
==============================

@rem Reads the config spec info
@if "%CC_CURRENT_COMPONENT%"=="chip_simulation" goto set_chip_sim_user_base
set USER_BASE=X:\pss\SW\prestera
goto set_user_base_done

:set_chip_sim_user_base
set USER_BASE=X:\simulation\chip_simulation

:set_user_base_done
set USER_MAKE=presteraTopMake

@rem Configure the tool
@rem If you want to use Diab tool comment the line with gnu
@rem and uncomment the line with diab
set TOOL=gnu


This is the result after replace the specific line, you can see that line "goto set_user_base_done" is also deleted
=========================================================

@rem Reads the config spec info
@if "%CC_CURRENT_COMPONENT%"=="chip_simulation" goto set_chip_sim_user_base
set USER_BASE=C:\TEMP\e

:set_chip_sim_user_base
set USER_BASE=X:\simulation\chip_simulation

:set_user_base_done
set USER_MAKE=presteraTopMake

@rem Configure the tool
@rem If you want to use Diab tool comment the line with gnu
@rem and uncomment the line with diab
set TOOL=gnu
 
Give this script a try (may need to edit it a bit to get in back to the proper state for your script since I had to make a couple changes for my testing). You'll need to declare a new string variable called NewLine which will hold the line you are inserting into the file. After the string is deleted from the text file, the script gets the length of the string to insert, adds two to account for the carriage return and linefeed, performs an finsblock to allocate that much space in the text file, then writes the string and the closing carriage return and linefeed.

proc main
string envBaseFile, LineInfo
integer Len=0
long Pos=0
string NewLine = "set USER_BASE=C:\TEMP\"

envBaseFile = "envBase.bat"
fopen 0 envBaseFile READWRITE
while NOT feof 0
fgets 0 LineInfo
if strfind LineInfo "Set USER_BASE="
strlen LineInfo Len
Pos = Len
fseek 0 (-Pos) 1
fdelblock 0 Len
strlen NewLine Len
finsblock 0 Len+2
fputs 0 NewLine
fputs 0 "`n`r"
exitwhile
endif
endwhile

fclose 0
endproc

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top