linux - grep ping 输出的持续时间

标签 linux bash grep ping

我想写一个 bash 代码,它只给我 ping 的时间。 我试过这个:

ping -4 www.google.com | grep -oh "time=* ms"

但我知道它不会给我 just 时间,但我认为它会减少到例如 time=30 ms。虽然它没有给我任何输出。

最佳答案

通过启用 -P 标志,在 GNU grep 上使用 PCRE 模式,单独提取毫秒值

ping -4 www.google.com | grep -oP ".*time=\K\d+" 

其中\K转义序列代表

\K: This sequence resets the starting point of the reported match. Any previously matched characters are not included in the final matched sequence.

或者你可以去掉任何需要的 GNU-ism 工具,只使用 POSIX sed 来做

ping -4 www.google.com | sed -n 's/.*time=\([[:digit:]]*\).*/\1/p'

关于linux - grep ping 输出的持续时间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49473085/

相关文章:

linux - Linux 上的 PDF : Combine font subsets and replace Type 3 with Type 1

linux - 反汇编为 Intel 语法的 x86 ELF 反汇编程序

bash - Crontab bash 脚本 : no such file or directory

linux - awk 在同一行打印输出

linux - 在我的 htaccess 中重定向导致 503 服务器太忙

bash - 使用ffmpeg在文件名中遇到空格时如何避免批量视频转码错误

bash - 是否可以在 Bash 脚本的 switch case 中组合两个条件?

linux - 如何grep非键盘字符?

regex - 如何 : Searching for a string in a file from the Windows command line?

linux - Bash 脚本 - if 和 grep 不起作用