using regexp to parse specific line

9 次查看(过去 30 天)
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

更多回答(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