using regexp to parse specific line

조회 수: 16 (최근 30일)
Marta  Casta~ño
Marta Casta~ño 2022년 10월 24일
댓글: Mathieu NOE 2022년 10월 24일
Hi,
I am trying to use regexp to parse different values of multiple lines in a file. A line looks like this:
{"timestamp": 1666599517.7899299, "cell_log_wdm0": "[/dev/cdc-wdm0] Successfully got signal info\nLTE:\n\tRSSI: '-65 dBm'\n\tRSRQ: '-15 dB'\n\tRSRP: '-97 dBm'\n\tSNR: '-3.2 dB'\n5G:\n\tRSRP: '-108 dBm'\n\tSNR: '7.5 dB'\n\tRSRQ: '-13 dB'\n"}
And I would like to get the timestamp, the name of the cell (wdm0), LTE RSRP and 5G RSRP. I am trying to use regexp but I am unsure of how to use it, or if it is even the right approach.
I would appreciate any help. :-)

채택된 답변

Mathieu NOE
Mathieu NOE 2022년 10월 24일
hello
I assumed there could be more than one line , but I also assumed that they would be written exactly the same way so I ended up with this code, that assumes that data are always located in the same cell.
fyi , attached the dummy text file, I only changed a few numeric values to check my code
all the best
clearvars
filename = "test2.txt";
C = readcell(filename,'Delimiter',{';',':',',',''''});
[m,n] = size(C);
for ck = 1:m
timestamp(ck) = C{ck,2};
name_cell = C{ck,3};
idx = strfind(name_cell,'_');
name_of_the_cell{ck} = name_cell(idx(end)+1:end);
Data_LTE_RSRP{ck} = C{ck,13};
Data_5G_RSRP{ck} = C{ck,20};
end
  댓글 수: 2
Marta  Casta~ño
Marta Casta~ño 2022년 10월 24일
Thanks, this worked perfectly!
Mathieu NOE
Mathieu NOE 2022년 10월 24일
My pleasure !

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

추가 답변 (1개)

chrisw23
chrisw23 2022년 10월 24일
편집: chrisw23 2022년 10월 24일
lines = readlines("c:\temp\lineLog.txt")
subLines = lines.split("\n").replace("\t","")
dataTs = subLines(:,1).split(",");
dataLte = array2table(subLines(:,3:6).extractAfter(":").replace("'","").strip,VariableNames=["RSSI" "RSRQ" "RSRP" "SNR"])
data5G = array2table(subLines(:,8:end-1).extractAfter(":").replace("'","").strip,VariableNames=["RSRP" "SNR" "RSRQ"])
table(dataTs,dataLte,data5G)
dataTs dataLte data5G
RSSI RSRQ RSRP SNR RSRP SNR RSRQ
_______________________________________________________________________________________________________ _______________________________________________ __________________________________
"{"timestamp": 1666599517.7899299" " "cell_log_wdm0": "[/dev/cdc-wdm0] Successfully got signal info" "-65 dBm" "-15 dB" "-97 dBm" "-3.2 dB" "-108 dBm" "7.5 dB" "-13 dB"
"{"timestamp": 1666599517.7899299" " "cell_log_wdm0": "[/dev/cdc-wdm0] Successfully got signal info" "-65 dBm" "-15 dB" "-97 dBm" "-3.2 dB" "-108 dBm" "7.5 dB" "-13 dB"
"{"timestamp": 1666599517.7899299" " "cell_log_wdm0": "[/dev/cdc-wdm0] Successfully got signal info" "-65 dBm" "-15 dB" "-97 dBm" "-3.2 dB" "-108 dBm" "7.5 dB" "-13 dB"
as long as the format is defined, this could be a starting point for your parser
I saved your example line (3times) to a text file before.

카테고리

Help CenterFile Exchange에서 String Parsing에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by