从文本文件中提取时间

192

因此,我尝试在Linux中编写Shell脚本,该脚本将从存储在文本文件中的Web服务器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”提取到“管道2”,将“ 86400数据包”提取到“ 86532481ms”行,我将非常感谢您的帮助!

毛林草坪

我用了这个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.

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章