Scan through a txt-file and find certain strings and replace certain lines by replacing strings

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 commentaires

Attaching file..will give you exact code....
Thanks for the extremely fast reply. However I couldn't find any files attached.

Connectez-vous pour commenter.

Réponses (1)

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)

Catégories

En savoir plus sur Characters and Strings dans Centre d'aide et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by