Reading ping output using readtable
2 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
dormant
el 8 de Feb. de 2024
Comentada: Walter Roberson
el 29 de Feb. de 2024
I'm creating a text file with ping results (on Linux) that looks like this (timestamp at start).
PING 172.17.102.60 (172.17.102.60) 56(84) bytes of data.
[1707403470.450935] 64 bytes from 172.17.102.60: icmp_seq=1 ttl=64 time=0.378 ms
[1707403471.452695] 64 bytes from 172.17.102.60: icmp_seq=2 ttl=64 time=0.248 ms
[1707403472.482987] 64 bytes from 172.17.102.60: icmp_seq=3 ttl=64 time=0.337 ms
I can read it using readtable:
511×9 table
Var1 Var2 Var3 Var4 Var5 Var6 Var7 Var8 Var9
_______________________ ____ _________ ________ __________________ ________________ __________ ______________ ______
{'[1707403470.450935]'} 64 {'bytes'} {'from'} {'172.17.102.60:'} {'icmp_seq=1' } {'ttl=64'} {'time=0.378'} {'ms'}
{'[1707403471.452695]'} 64 {'bytes'} {'from'} {'172.17.102.60:'} {'icmp_seq=2' } {'ttl=64'} {'time=0.248'} {'ms'}
{'[1707403472.482987]'} 64 {'bytes'} {'from'} {'172.17.102.60:'} {'icmp_seq=3' } {'ttl=64'} {'time=0.337'} {'ms'}
How can I extract the timestamps from Var1 and the times from Var8? I think I should be using cellfun, but I don't know how to.
4 comentarios
Walter Roberson
el 29 de Feb. de 2024
ping sometimes produces results such as
Request timeout for icmp_seq 2
In theory your code has to handle these as well.
Respuesta aceptada
Voss
el 8 de Feb. de 2024
Here's one way to get the timestamps and times directly out of the file (without using readtable):
format longg
filename = 'test_file.txt';
% show the file's contents, for reference
type(filename)
% read and parse the file
C = regexp(fileread(filename),'\[([\d\.?]+)\].*?time=([\d\.?]+)','tokens');
data = str2double(vertcat(C{:}))
timestamps = datetime(data(:,1),'ConvertFrom','posix')
times = data(:,2)
0 comentarios
Más respuestas (1)
Stephen23
el 8 de Feb. de 2024
Editada: Stephen23
el 8 de Feb. de 2024
Here is an easy way to import all numeric data as numeric, and with minimal post-processing for the datestamp:
T = readtable("testping.txt", 'Delimiter',[" ","=","]","["], "LeadingDelimitersRule","ignore", "MultipleDelimsAsOne",true)
T.Var1 = datetime(T.Var1, "ConvertFrom","posixtime")
0 comentarios
Ver también
Categorías
Más información sobre Environment and Settings en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!