Reading ping output using readtable

조회 수: 2 (최근 30일)
dormant
dormant 2024년 2월 8일
댓글: Walter Roberson 2024년 2월 29일
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
dormant
dormant 2024년 2월 29일
Thanks. I was late back to check. Sometimes my work gets me distracted.
Walter Roberson
Walter Roberson 2024년 2월 29일
ping sometimes produces results such as
Request timeout for icmp_seq 2
In theory your code has to handle these as well.

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

채택된 답변

Voss
Voss 2024년 2월 8일
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)
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
% read and parse the file
C = regexp(fileread(filename),'\[([\d\.?]+)\].*?time=([\d\.?]+)','tokens');
data = str2double(vertcat(C{:}))
data = 3×2
1.0e+00 * 1707403470.45093 0.378 1707403471.45269 0.248 1707403472.48299 0.337
timestamps = datetime(data(:,1),'ConvertFrom','posix')
timestamps = 3×1 datetime array
08-Feb-2024 14:44:30 08-Feb-2024 14:44:31 08-Feb-2024 14:44:32
times = data(:,2)
times = 3×1
0.378 0.248 0.337

추가 답변 (1개)

Stephen23
Stephen23 2024년 2월 8일
편집: Stephen23 2024년 2월 8일
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 = 3×12 table
Var1 Var2 Var3 Var4 Var5 Var6 Var7 Var8 Var9 Var10 Var11 Var12 __________ ____ _________ ________ __________________ ____________ ____ _______ ____ ________ _____ ______ 1.7074e+09 64 {'bytes'} {'from'} {'172.17.102.60:'} {'icmp_seq'} 1 {'ttl'} 64 {'time'} 0.378 {'ms'} 1.7074e+09 64 {'bytes'} {'from'} {'172.17.102.60:'} {'icmp_seq'} 2 {'ttl'} 64 {'time'} 0.248 {'ms'} 1.7074e+09 64 {'bytes'} {'from'} {'172.17.102.60:'} {'icmp_seq'} 3 {'ttl'} 64 {'time'} 0.337 {'ms'}
T.Var1 = datetime(T.Var1, "ConvertFrom","posixtime")
T = 3×12 table
Var1 Var2 Var3 Var4 Var5 Var6 Var7 Var8 Var9 Var10 Var11 Var12 ____________________ ____ _________ ________ __________________ ____________ ____ _______ ____ ________ _____ ______ 08-Feb-2024 14:44:30 64 {'bytes'} {'from'} {'172.17.102.60:'} {'icmp_seq'} 1 {'ttl'} 64 {'time'} 0.378 {'ms'} 08-Feb-2024 14:44:31 64 {'bytes'} {'from'} {'172.17.102.60:'} {'icmp_seq'} 2 {'ttl'} 64 {'time'} 0.248 {'ms'} 08-Feb-2024 14:44:32 64 {'bytes'} {'from'} {'172.17.102.60:'} {'icmp_seq'} 3 {'ttl'} 64 {'time'} 0.337 {'ms'}

카테고리

Help CenterFile Exchange에서 Environment and Settings에 대해 자세히 알아보기

제품


릴리스

R2023b

Community Treasure Hunt

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

Start Hunting!

Translated by