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 SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Replace first 16 characters of a line with new characters

Status
Not open for further replies.

ok1397

Technical User
Feb 7, 2005
70
0
0
US
Hi everyone, please help !!! i have a text file which a script searches for a specific string. If the first 16 characters of a line matches the criteria, delete only the first 16 characters of that line and replace with new 16 characters, without deleting the rest of the line. Can this be done?
 
Yep, this is pretty easily done, basically you would need to roll back the file pointer to the beginning of the line, delete those 16 characters, insert the new string, and save the file.

It looks like I don't quite have a sample script on my site that does this, but you might check out the link below starting at the fseek command.


 
I know there will be simpler ways...
but (A) I couldn't work out how to back up the file pointer and (B) this works, so I didn't bother.

This sample below will Filter through "C:\Program Files\Symantec\Procomm Plus\Capture\tempfile.txt"
It searches for "0123456789ABCDEF" as the 1st 16 chars of any line.
Each line of the file is written to "swapfile.txt".
If the 1st 16 chars match, that line is written with the replacement characters "_THE_NEW_STRING_".

The second portion overwrites the original file.




proc main
integer LengthStr
string FNameAA = "C:\Program Files\Symantec\Procomm Plus\Capture\tempfile.txt"
string FNameBB = "C:\Program Files\Symantec\Procomm Plus\Capture\swapfile.txt"

string LineInfo

;create a second file with the replacement string
if isfile FNameAA ; Make sure file exists.
if fopen 0 FNameAA READ ; Open file for read.
fopen 1 FNameBB CREATE
while not feof 0 ; Loop to end of file.
fgets 0 LineInfo ; Get line from file.

strlen LineInfo LengthStr ;get Length of Line

if rstrcmp LineInfo "0123456789ABCDEF" 16 ;1st 16 chars match

strupdt LineInfo "_THE_NEW_STRING_" 0 16

endif

fputs 1 LineInfo ;write the string to other file.

endwhile

fclose 0 ; Close the file.
fclose 1 ; Close the file.
endif

else
errormsg "File doesn't exist."

endif



;swap the files....
if isfile FNameBB ; Make sure file exists.
if fopen 0 FNameBB READ ; Open file for read.
fopen 1 FNameAA CREATE ; Blank out the existing file.
while not feof 0 ; Loop to end of file.
fgets 0 LineInfo ; Get line from file.

fputs 1 LineInfo

endwhile

fclose 0 ; Close the file.
fclose 1 ; Close the file.
endif

else
errormsg "File doesn't exist."

endif


endproc

 
Thank you knob and comtechau for your help, i was able to get the script working using fseek to roll back pointer and making changes to the file. Thanks for all your help.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top