Scan through a txt-file and find certain strings and replace certain lines by replacing strings
Show older comments
Hi, It's been a while since I've used Matlab and I just couldn't figure out how to make this work.
I am trying to edit a dxf-file (saved in .txt of course) and it is very important not to modify the structure of the code - at all! I want to scan the file line-by-line and find the lines which match the string str1='E_FROM'. After the scanning I should change spesific lines near the line which matches the string str1. I think it is wisest to first scan through the document and store the lines I am searching for. Now that I know where those lines are I want to modify the document. In my case I want to erase the string 14 lines above the "E_FROM" line. How this should be done. Please, help!
-Kalle
2 Comments
KSSV
on 21 Nov 2017
Attaching file..will give you exact code....
Kalle Palola
on 21 Nov 2017
Answers (1)
Are Mjaavatten
on 21 Nov 2017
fid = fopen('in.txt');
lines = textscan(fid,'%s','delimiter','\n');
fclose(fid);
lines = lines{1};
relevant = find(~cellfun(@isempty,strfind(s{1},'E_FROM')));
for i = 1:length(relevant)
lines{relevant(i)-14} = <modified line>;
end
fid = fopen('out.txt','w');
for i = 1:length(lines)
fprintf(fid,'%s\n',lines{i});
end
fclose(fid)
Categories
Find more on Characters and Strings 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!