find a string in a text file and to check particular character
4 次查看(过去 30 天)
显示 更早的评论
I have following texts in a file:
...
RECORD_A
BE_FUNC
1; "scalar"; 1.0; 0.0; 2; 0
ACCEPT;ACCEPT
9; 1; 0; 0; 0
ACCEPT;ACCEPT
-5; 1;5.1; 0; 0
EN_FUNC
GIBT_ER
...
In this file 'RECORD_A' , 'BE_FUNC', 'EN_FUNC' , 'GIBT_ER' are unique strings . I am copying each line from a file and printing to another file.(let line number of BE_FUNC is 'n'). Now, I want to check if the line is equal to ' BE_FUNC ' ,then i want to check what is there in the 5th position (delimiter ';') in the next line (that is line n+1, here it is : 1; "scalar"; 1.0; 0.0; 2; 0 and the values are varying depends on the some other inputs ) and at the same time i have to print BE_FUNC to the other file as told already. And if it is '2' i have to check line n+3 and n+5 line (but need to print n,n+1,n+2,n+3,n+4,n+5 etc. as it is ) if it is '3' i have to check n+3,n+5,n+7 line (but need to print n,n+1,n+2,n+3,n+4,n+5,n+6,n+7 etc. as it is ).
main problem is : i have to complete one loop to print 'BE_FUNC' before going to n+1 line.
Here is my uncompleted program:
fid_old= fopen('dummyfile.txt','r+'); %read from temp file
fid_new= fopen ('newfile.txt','wt'); % creating a new file
while 1
tline = fgetl(fid_old);
if ~ischar(tline)
break
end
if strfind(tline,'BE_FUNC')==1
disp('Here your help needed, Thank you')
end
fprintf(fid_new,tline);
fprintf(fid_new,'\n');
end
fclose(fid_old);
fclose(fid_new);
And Everything in a single while loop is pretty much expected.
0 个评论
回答(1 个)
Sreeram Mohan
2014-9-23
Hi Vipin,
Have you tried using strtok ? This might be more useful in the case you are dealing with.
Thanks,
Sreeram Mohan
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Environment and Settings 的更多信息
产品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!