Matlab using regexp function i want to parse string and data

Hi
I have a string :"2019-05-10T21:42:59.980043+00:00 ERR kernel: [ 82.127095] sof-audio-pci 0000:00:1f.3: error: can't find message header 0x0"
I want to parse the data Like using regexp
ERR_Kernel = [82]
sof-audio-pci = 0000:00:1f.3
error.can't find message header = 0x0
Thanks

댓글 수: 4

What does your code look like so far?
Hi Bob
Thanks for the acknowledgement
My code is as follows
fid=fopen('messages');
KERNEL = 'INFO kernel';
pattern1 = 'ERR kernel:\s(\d+).*?' ;
pattern2 = 'sof-audio-pci \s([\d\.]+).*?';
pattern3 = 'error:\s(\d+).*?;
pattern4 = 'header\s(\d+).*?';
y = 0;
tline =fgetl(fid)
while feof(fid) == 0
tline = fgetl(fid);
match = findstr(tline,KERNEL);
s_data = length(match);
if s_data > 0
y = y+s_data;
tokens = regexp(tline, pattern1, 'match') ;
tokens = regexp(tline, pattern2, 'match') ;
tokens = regexp(tline, pattern3, 'match') ;
tokens = regexp(tline, pattern4, 'match') ;
Data.Out = reshape(str2double([tokens{:}]), 1, []).' ;
disp(sprintf('%d:%s:%d\n',y,tline,Data.Out));
end;
end
fclose(fid);
% Desired OutPut format
DataOutPut.ERR kernel = [82.127095];
t = DataOutPut.ERR kernel;
DataOutPut.ERR kernel.sof_audio_pci = [0000:00:1f.3];
DataOutPut.ERR kernel.sof_audio_pci.error.message header = [0x0];
subplot(2,2,1);Plot(t,DataOutPut.ERR kernel.sof_audio_pci,'-');
subplot(2,2,2);Plot(t,DataOutPut.ERR kernel.sof_audio_pci.error.message header,'*');
Thanks a lot in advance
Sriram
Unfortunately time that includes timezone offset is not enough to specify which time zone should be used. Considering that the time zone offset is 00:00 then can we assume UTC?
Hi Walter
But ignonre the TimeZone , can you please suggest using regexp function what should be the pattern I should use to parse the Data .
Data.OutPut1 = ERR kernel: [ 82.127095]
Data.OutPut2 = sof-audio-pci 0000:00:1f.3
Data.OutPut3 = error: can't find message header 0x0

댓글을 달려면 로그인하십시오.

 채택된 답변

Bob Thompson
Bob Thompson 2019년 5월 22일

0 개 추천

There are a few things I notice here.
1) I have edited your patterns so they should capture what you're looking for.
2) It seems like you don't have any indexing on 'tokens' in your while loop. This is not necessarily a problem except when you have more than one set of values able to be returned for a single tline. You should index the different possible token results.
3) It seems like you just want the numbers from the different lines, not the text themselves. To do this just use 'tokens' instead of 'match' in your regexp command. Also, be sure to change your tokens variable name to something else to prevent any naming errors.
4) What is the purpose of pattern3? You don't keep any of the results for later use, so why bother finding it?
pattern1 = 'ERR kernel:\s.\s*(\d+.\d+).';
pattern2 = 'sof-audio-pci (\d+.\d+.\d\w.\d)';
pattern4 = 'header (\d\w\d)';

댓글 수: 4

Life is Wonderful
Life is Wonderful 2019년 5월 23일
편집: Life is Wonderful 2019년 5월 23일
Hi Bob,
It works for me.
Point 2: You are right I am doing a loop . Initially I want to parse single line followed by multiple lines.
Sure , I need your help in indexing over all instances while running the loop.
Point 3 :Yes, I would like to have indexing with different name but I don't know how to get it.
Point 4: Pattern3 was used to parse all the text following "error: can't find message header" .
In the end , I would like to generate plot using cellfun@plot
To make things simpler , Is Ok if I share my full input file .
As I'm sure you know, indexing is very important within matlab. If you are unsure of the basics of indexing a more complete guide to indexing can be found here.
Indexing in while loops is not immediately obvious, but the general concept is to create an index within the loop. Because you have an if condition make sure you put the index within the true condition, otherwise you will end up with a bunch of extra empty elements.
c = 0;
while feof(fid) == 0
if s_data>0
c = c + 1;
tokens{c,1} = ...
tokens{c,2} = ...
tokens{c,3} = ...
tokens{c,4} = ...
end
end
This does introduce another layer of cells that can be a pain to deal with, but it should get you started on how you might index things within your loop.
Are you saying that you want to capture the entire error message? If so, try something like this:
pattern3 = 'error: [\w\s]+ header'; % Couple with a 'match' for entire message
What exactly are you looking to plot? What sets of data are you comparing? (Independent and dependent variables?)
The first step in this will be to convert your data to numbers. I think it's technically possible to plot strings, but I don't know how much success you will have with it.
You are always welcome to upload your input file. I personally won't be able to make use of it, but the greater community might.
I don't know how much I personally will be able to help with this matter. I would suggest posting a new question outlining your desire for help with the plotting.
sriram shastry comments to Bob Nbob:
Thanks a lot for all the help.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Characters and Strings에 대해 자세히 알아보기

태그

질문:

2019년 5월 21일

편집:

2019년 5월 25일

Community Treasure Hunt

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

Start Hunting!

Translated by