Replacing a string with another string in a text file

My file reads something like this:
"name of unique string"
x= 10, 1, 0, 1, 10
y= 500, 50, 0, 50 500
Now I know how to locate the "name of unique string" BUT what I want to replace is the "y= 500,50,0,50,500".
Can someone help me out with this please?
Thanks in advance, Aniruddh

2 Comments

Please post some of the code that you are using to solve this problem. Are you reading in each line of the file and doing some sort of strfind to determine where the unique string is? Do you want to replace only those lines that are y= or just this one in particular?
My main problem is that the "x=" and "y=" are repeated many times in the file and in some cases have identical values. But I also know that the "y=" line occurs exactly 4 lines after the "unique string".
fi=fopen('properties.txt','r');
fo=fopen('properties_new.txt','w');
while ~feof(fi)
l=fgets(fi);
if strfind(l,'name of unique string')
(here I need to add the code to change the 4th line after the position of the "unique string" which I can't figure out how to do)
end
end
I hope this answers your question.

Sign in to comment.

 Accepted Answer

The thing about sequential files is that they're, well..."sequential"
You've got a couple of choices -- read the full file as character data and operate in memory to change the 3rd record (determined either by knowing it is the nth record or a search) and then writing the modified memory to a new (or overwrite the existing) file. NB: make backups while testing the latter!!!
Alternatively and for short files probably the easier is to read the file a line at a time, ( fgetl is your friend here) and echoing that line back to a new file until you find the line in question at which time you make the desired change and write the modified record. Then, of course, complete to feof on the file.
fidi=fopen('inputfile','r');
fido=fopen('outputfile','w');
while ~feof(fidi)
l=fgetl(fidi); % read line
if strfind(l,'y=')
% modify line here
l='whatever new string';
end
fprintf(fido,'%s',l) % 'fgetl returns \n so it's embedded
end
fidi=fclose(fidi);
fido=fclose(fido);
Salt to suit...

1 Comment

Thank you for your reply. I will try your suggestion and let you know!

Sign in to comment.

More Answers (1)

dpb
dpb on 4 Jun 2014
Edited: dpb on 4 Jun 2014
... I also know that the "y=" line occurs exactly 4 lines after the "unique string"
while ~feof(fi)
l=fgetl(fi);
if strfind(l,'name of unique string')
for i=1:3, fgetl(fi); end % skip the three intervening
l=fgetl(fi);
% make whatever changes to 'l' need here
...
NB: depending again on what you're trying to do, the above doesn't echo the intervening lines back out to the output file but just discards them...

3 Comments

Thanks, I don't want to discard them, but at least this will get me to the "y=" which I can edit now.
Thank you again, Aniruddh
One more small thing, is there a way I can just edit the original file and not have a separate output file? I know I'll have to use the 'r+' permission, and was trying to use the strrep command but its not working with my code at least.
Aniruddh
Edit:Nevermind good Sir, I found another post that told me its not possible!
As I said in my opening statement, The thing about sequential files is that they're, well..."sequential"
IF and only if the string and it's replacement were of the identical length, then "yes, Virginia, there is a Santa Claus" and you could do the replacement in situ. But, the first restriction is generally too restrictive and even if not the extra effort to write the code to do so is generally counter-productive.
If you want to "edit the original file", then the far simpler way is to use a regular text editor in batch mode.

Sign in to comment.

Categories

Products

Asked:

on 2 Jun 2014

Commented:

dpb
on 5 Jun 2014

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!