How to change a specific part in a specific line in a text file?
Show older comments
Hi,
I have a txt file and I need to change specific numbers in specific lines,
line 131, a = 10
line 132, b = 30
is any way to change the above lines to:
a = 5
b = 60
2 Comments
Shahen
on 1 Jun 2015
Walter Roberson
on 1 Jun 2015
'\d+' to recognize only decimal digits
'\d+(\.\d*)?' to recognize decimal digits that might optionally be followed by a '.' that in turn might be followed by digits
If the number might be negative, start the pattern with '-?'
If the number might have floating point exponent then it starts to get ugly to write the expression.
Accepted Answer
More Answers (2)
Thorsten
on 12 May 2015
Use
patchline('test.txt', 'a = 5', 132)
patchline('test.txt', 'b = 60', 133)
using my function
function patchline(filename, newstring, lineno)
%PATCHLINE
%
%Thorsten.Hansen@psychol.uni-giessen.de
fid1 = fopen(filename, 'r');
if fid1 == -1
error(['Cannot open file ' filename ' for reading.']);
end
tempfilename = 'temp1.txt';
fid2 = fopen(tempfilename, 'w');
if fid2 == -1
error(['Cannot open file ' filename ' for writing.']);
end
line = fgets(fid1);
i = 1;
while line ~= -1
if i == lineno
fprintf(fid2, '%s\n', newstring);
else
fprintf(fid2, '%s', line);
end
line = fgets(fid1);
i = i + 1;
end
st = fclose(fid1);
if st == -1
error(['Cannot close ' filename '.'])
end
st = fclose(fid2);
if st == -1
error(['Cannot close ' tempfilename '.'])
end
if i < lineno
warning(['File ' filename ' has fewer than ' int2str(lineno) 'lines.']);
delete(tempfilename)
else
movefile(tempfilename, filename, 'f')
end
2 Comments
Shahen
on 12 May 2015
Walter Roberson
on 12 May 2015
patchline('test.txt', sprintf('a = %d',a), 132)
Joseph Cheng
on 11 May 2015
Edited: Joseph Cheng
on 11 May 2015
0 votes
you'll need to use fopen, fgetl, and fprintf() to do this. To accomplish this is that you'll need to open the file in read/write mode and jump to the first specific line (in your example 131) and do your edit by overwriting that line. Then all subsequent lines will have to be rewritten. You cannot just make an edit to the raw file like you would in a text editor. otherwise you'll either end up with additional spaces or lines that are "overlapping".
For example your original line of "a= 10" is say 5 characters long before the new line + carriage return (\n\r). but you want to replace it with "a= 5" which is only 4 characters long. this wouldn't be too big of an issue since you can probably live with an extra " " somewhere. The issue comes in when you are trying to put something like "a= 123" which is longer than the original. Since the inserted text doesn't move all the items below it you'll be writing into the next line's data.
4 Comments
Joseph Cheng
on 11 May 2015
Edited: Joseph Cheng
on 11 May 2015
since you're reading it line by line strrep isn't going to get you anything that replacing the whole string. strrep replaces all occurrences of the string with another string. Also as far as i know strrep only replaces a subset of the original string when it is loaded into the workspace and not in a file.
teststr = ['abcabcabc']
strrep(teststr,'a','z')
%returns ['zbczbczbc']
so you already have teststr you already know what it should be.
Since you do not explain how it is changeable here is the structure of reading and skipping to lineX.
fid = fopen('myfile.txt')
linenum = 0;
while ~eof(fid)
tline = fgetl(fid);
linenum = linenum+1;
if linenum>=131
if linenum==131
fprintf(fid,'a = %f',%whatever you want it to swap to)
elseif linenum==132
fprintf(fid,'b = %f',%whatever you want it to swap to)
else
fprintf(fid,tline)
end
end
I don't have matlab with me to check the code above with an example but with the documentation on fprintf and other functions it will get you where you need to go.
Walter Roberson
on 12 May 2015
You should always be testing for end of file after you read, not before you read. feof() is not predictive. feof() is not true until you make a read that fails.
Categories
Find more on File Operations in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!