find a string in a text file and to check particular character

2 views (last 30 days)
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.

Answers (1)

Sreeram Mohan
Sreeram Mohan on 23 Sep 2014
Hi Vipin,
Have you tried using strtok ? This might be more useful in the case you are dealing with.
Thanks,
Sreeram Mohan
  1 Comment
Vipin Mohan
Vipin Mohan on 23 Sep 2014
Edited: Vipin Mohan on 23 Sep 2014
hi, the problem is i have to print it on the same time !!! I have to check and print 'BE_FUNC', and if it yes, i have to go to next line to find out the character '2' in the next line..and strtok can be used in the n+1 line only... before that i have to print!!!!!! i have to complete one loop to print 'BE_FUNC' before going to n+1 line.

Sign in to comment.

Categories

Find more on Data Import and Export in Help Center and File Exchange

Products

Community Treasure Hunt

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

Start Hunting!