用grep和awk过滤ping结果完全不起作用 [英] filter ping result with grep and awk altogether doesn't work

查看:238
本文介绍了用grep和awk过滤ping结果完全不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想仅将ping输出及其延迟时间通过管道传递给文本. 当我这样做的时候,我得到了预期的结果

I want to pipe the ping output with only its delay time to a text. while I do , I get this as expected

ping somesite 
PING somesite  (220.181.57.217) 56(84) bytes of data.
64 bytes from 220.181.57.217: icmp_seq=1 ttl=52 time=43.4 ms
64 bytes from 220.181.57.217: icmp_seq=2 ttl=52 time=43.7 ms
64 bytes from 220.181.57.217: icmp_seq=3 ttl=52 time=43.4 ms

然后我这样做

ping somesite | awk -F '[:=]'  '{print $5}'

43.3 ms
43.2 ms
43.3 ms
43.2 ms
43.2 ms
43.1 ms
43.1 ms
43.3 ms
43.2 ms
43.6 ms
43.3 ms
43.3 ms

所以第一行是空白,然后我认为必须先摆脱它.

So The first line is blank, then I figured I had to get rid of it first.

ping somesite | grep -v "PING" | awk -F '[:=]'  '{print $5}'

但是没有任何输出.

ping somesite | grep -v "PING"

 ping somesite | awk -F '[:=]'  '{print $5}'

它将起作用.将它们放在一起时.不会的 我想知道原因.

It will work. When putting them together. It won't. I'd like to know the reason for this.

推荐答案

grep命令中使用grep 进行块缓冲时,您没有得到任何输出.

You're not getting any output when piping with grep due to block buffering in grep command.

您可以使grep使用行缓冲,以便获取每行的输出:

You can make grep use the line buffering so get the output for each line:

ping google.com | grep --line-buffered -v "PING" | awk -F '[:=]' '{print $5}'

但是您不需要grep ,因为您可以使用单个awk:

ping google.com | awk -F '[:=]' 'NR>1{print $5}'

这篇关于用grep和awk过滤ping结果完全不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆