从文本文件中提取时间

从文本文件中提取时间

因此,我尝试在 Linux 中编写 shell 脚本,该脚本将从存储在文本文件中的网络服务器 ping 中提取往返时间。所以我基本上拥有的是一个文本文件:

    PING e11699.b.akamaiedge.net (104.100.153.112) 56(84) bytes of data.
    64 bytes from a104-100-153-112.deploy.static.akamaitechnologies.com (104.100.153.112): icmp_seq=1 ttl=60 time=17.2ms
    64 bytes from a104-100-153-112.deploy.static.akamaitechnologies.com (104.100.153.112): icmp_seq=2 ttl=60 time=12.6ms
    64 bytes from a104-100-153-112.deploy.static.akamaitechnologies.com (104.100.153.112): icmp_seq=3 ttl=60 time=11.7ms
    ... (a bunch more ping responses here)
    --- e11699.b.akamaiedge.net ping statistics ---
    86400 packets transmitted, 86377 received, 0% packet loss, time 86532481ms
    rtt min/avg/max/mdev = 6.281/18.045/1854.971/28.152 ms, pipe 2

因此,我最终使用 sed 尝试从文本文件中仅提取 17.2、12.6、11.7 和更多次。下面是我的 sed 行:

    sed 's/.*(time=\([0-9]*\(\.[0-9]*\)\{0,1\}\) ms/\1/' pingoutput.txt | sort -n > sortedtime.txt 

此行成功地提取并排序了我需要的所有时间,但它还从我不需要的 ping 文本文件中提取了几行。它创建的文本文件如下所示:

--- e11699.b.akamaiedge.net ping statistics ---
86400 packets transmitted, 86377 received, 0% packet loss, time 86532481ms
rtt min/avg/max/mdev = 6.281/18.045/1854.971/28.152 ms, pipe 2
11.7
12.6
17.2
...
86400 packets transmitted, 86377 received, 0% packet loss, time 86532481ms

如果无论如何可以避免提取文本文件中不需要的“---e11699”到“pipe 2”和“86400 packet”到“86532481ms”行,我将非常感谢您的帮助!

答案1

我用了这个 sed:

sed -n 's/.*time=\([0-9].*\)ms.*/\1/p' times | sort -n

在您的示例文件 ( times) 上:

PING e11699.b.akamaiedge.net (104.100.153.112) 56(84) bytes of data.
64 bytes from a104-100-153-112.deploy.static.akamaitechnologies.com (104.100.153.112): icmp_seq=1 ttl=60 time=17.2ms
64 bytes from a104-100-153-112.deploy.static.akamaitechnologies.com (104.100.153.112): icmp_seq=2 ttl=60 time=12.6ms
64 bytes from a104-100-153-112.deploy.static.akamaitechnologies.com (104.100.153.112): icmp_seq=3 ttl=60 time=11.7ms
... (a bunch more ping responses here)
--- e11699.b.akamaiedge.net ping statistics ---
86400 packets transmitted, 86377 received, 0% packet loss, time 86532481ms
rtt min/avg/max/mdev = 6.281/18.045/1854.971/28.152 ms, pipe 2

我得到这个结果:

sed -n 's/.*time=\([0-9].*\)ms.*/\1/p' times | sort -n
11.7
12.6
17.2


我用这个-n开关去掉了不需要的线条。从man sed

-n By default, each line of input is echoed to the standard output after all of the commands have been applied to it. The -n option suppresses this behavior.

答案2

如果grep使用pcre正则表达式没问题:

$ grep -oP 'time=\K[0-9.]+' ip.txt | sort -n
11.7
12.6
17.2
  • -o只打印匹配的模式
  • -P使用 PCRE 正则表达式
  • time=\K正向回顾,不是输出的一部分
  • [0-9.]+一个或多个数字和.字符
  • sort -n按数字排序

单独使用perl

$ perl -nle 'push(@a,/time=\K([0-9.]+)/g); END{ print foreach sort {$a <=> $b} @a }' ip.txt 
11.7
12.6
17.2
  • 这里用匹配的模式填充一个数组,然后在最后打印出按数字排序的数组

答案3

grep 'time=' pingoutput.txt | awk '{print $8}'

相关内容