Parsing unique .txt messages issue in matlab?!

3 次查看(过去 30 天)
Hello world of matlab programming .
I am trying and struggling to parse a series messages from text file that has different unique patterns and save them as txt files using matlab programming.
I have as input txt file:
[#11:23][INFO][0x0015a] it's here and it's optimally required start index[1] , length[15]
INFO isn't NULL
[#12:25][PERFECT][0x0015a] process returned as NULL load index[1] , length[20] , type[0]
[#13:3][INFO][0x0015a] it's here and it's optimally required start index[1] , length[15]
PERFECT isn't NULL
[#4:23][INFO][0x0015a] it's here and it's optimally required start index[1] , length[15]
Time is here [Tick:135055] , Time: 17, index: 608, CastedType:20002, area :0
[#15:23][INFO][0x0015a] it's here and it's optimally required start index[1] , length[15]
[#16:25][PERFECT][0x0015a] process returned as NULL load index[1] , length[20] , type[0]
[#17:3][INFO][0x0015a] it's here and it's optimally required start index[1] , length[15]
[#8:23][INFO][0x0015a] it's here and it's optimally required start index[1] , length[15]
time is here [Tick:135055] , Time: 17, index: 608, CastedType:20002, area :0
[#16:23][INFO][0x0015a] it's here and it's optimally required start index[1] , length[15]
[#14:25][PERFECT][0x0015a] process returned as NULL load index[1] , length[20] , type[0]
[#18:3][INFO][0x0015a] it's here and it's optimally required start index[1] , length[15]
[#6:23][INFO][0x0015a] it's here and it's optimally required start index[1] , length[15]
Time is here [Tick:135055] , Time: 17, index: 608, CastedType:20002, area :0
this is the type formats of all rows that txt have , so each row is repeated on given txt file and it has its own unique pattern as I showed above, where the key words [INFO] , [PERFECT] are not changed per the message those key words values are not changed in this message pattern.
consider each row is a new message , so at each row there is a new message starts irrelative to the other messages.
what Im trying to implement in matlab a function that reads line by line the given txt file to a function as input and all rows there has different patterns of messages as I mentioned above and to dump all rows / messages that has the certain type of keyword [PERFECT] :
[#12:25][PERFECT][0x0015a] process returned as NULL load index[1] , length[20] , type[0]
to another txt file. so then if I go to another txt file I shall see all rows there has this type of messages:
[#12:25][PERFECT][0x0015a] process returned as NULL load index[1] , length[20] , type[0]
Now after sniffing this type of message that has keyword [PERFECT] from the given txt(input txt) , I need to read line by line the new txt file that I generated that has the certain message type and then take the load index values and dump them in another txt file that has just the values of load index.
Function description:
Given txt file to the function as input that txt file has all messages patterns that I mentioned above and at each row there is new message:
[#11:23][INFO][0x0015a] it's here and it's optimally required start index[1] , length[15]
INFO isn't NULL
[#12:25][PERFECT][0x0015a] process returned as NULL load index[1] , length[20] , type[0]
[#13:3][INFO][0x0015a] it's here and it's optimally required start index[1] , length[15]
PERFECT isn't NULL
[#4:23][INFO][0x0015a] it's here and it's optimally required start index[1] , length[15]
Time is here [Tick:135055] , Time: 17, index: 608, CastedType:20002, area :0
[#15:23][INFO][0x0015a] it's here and it's optimally required start index[1] , length[15]
[#16:25][PERFECT][0x0015a] process returned as NULL load index[1] , length[20] , type[0]
[#17:3][INFO][0x0015a] it's here and it's optimally required start index[1] , length[15]
[#8:23][INFO][0x0015a] it's here and it's optimally required start index[1] , length[15]
time is here [Tick:135055] , Time: 17, index: 608, CastedType:20002, area :0
[#16:23][INFO][0x0015a] it's here and it's optimally required start index[1] , length[15]
[#14:25][PERFECT][0x0015a] process returned as NULL load index[1] , length[20] , type[0]
[#18:3][INFO][0x0015a] it's here and it's optimally required start index[1] , length[15]
[#6:23][INFO][0x0015a] it's here and it's optimally required start index[1] , length[15]
Time is here [Tick:135055] , Time: 17, index: 608, CastedType:20002, area :0
Results/output of the function:
1. Generating txt file that has all rows of the **certain pattern** that I explained above (all rows that has word **[PERFECT]** so the generated txt file shall be having all messages / rows that has **[PERFECT]** :
[#12:25]**[PERFECT]**[0x0015a] process returned as NULL load index[1] , length[20] , type[0]
[#16:25]**[PERFECT]**[0x0015a] process returned as NULL load index[1] , length[20] , type[0]
[#14:25]**[PERFECT]**[0x0015a] process returned as NULL load index[1] , length[20] , type[0]
2. Then generating a another new txt file for the load index values which in my case load index values found inside [ ] of the word load index ( load index [value] ), so the function shall dump in new txt file the values of the load index **as column** into the another new generated txt file :
1
1
1
How to parse and implement in matlab that function functionality?
thanks alot for any cooperation !
  2 个评论
Image Analyst
Image Analyst 2021-12-14
Make it easy for people to help you, not hard. Attach all needed text files with the paperclip icon.
Jimmy cho
Jimmy cho 2021-12-14
@Image Analyst I understand, thanks for your advice and will be adapted ! ! !

请先登录,再进行评论。

采纳的回答

Voss
Voss 2021-12-14
fid_in = fopen('input.txt','r');
fid_out = fopen('perfect.txt','w');
fid_index = fopen('load_index.txt','w');
while true
line = fgetl(fid_in);
if isequal(line,-1)
break
end
if ~contains(line,'[PERFECT]')
continue
end
fprintf(fid_out,'%s\n',line);
idx1 = strfind(line,'load index[')+11;
idx2 = find(line(idx1+1:end) == ']',1)+idx1-1;
fprintf(fid_index,'%s\n',line(idx1:idx2));
end
fclose(fid_in);
fclose(fid_out);
fclose(fid_index);
  4 个评论
Voss
Voss 2021-12-14
You also have to change +11 to +7 in the line with strfind, because strfind returns the index of the start of the string (e.g., 'load index[' or 'length[') and we need to start looking for the ']' character after the end of the string. That part can be generalized to look for any word and taking into account the length of the word you're looking for.
fid_in = fopen('input.txt','r');
fid_out = fopen('perfect.txt','w');
fid_index = fopen('load_index.txt','w');
while true
line = fgetl(fid_in);
if isequal(line,-1)
break
end
if ~contains(line,'[PERFECT]')
continue
end
fprintf(fid_out,'%s\n',line);
idx1 = strfind(line,'length[')+7;
idx2 = find(line(idx1+1:end) == ']',1)+idx1-1;
fprintf(fid_index,'%s\n',line(idx1:idx2));
end
fclose(fid_in);
fclose(fid_out);
fclose(fid_index);
Jimmy cho
Jimmy cho 2021-12-14
much appreciated !!
very clearn and useful answer !

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Logical 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by