grep&& awk组合导致bash

时间:2016-05-19 07:00:25

标签: bash shell grep cut

为什么ping -c 1 google.com | grep "bytes from" | cut -d = -f 1会返回64 bytes from 216.58.216.206: icmp_seq而不是0 ttl?我希望-f 1返回0 ttl

2 个答案:

答案 0 :(得分:3)

您可以使用awk完成所有操作并避免grepcut

ping -c 1 google.com | awk -F= '/bytes from/{print $2}'
0 ttl

btw你的命令应该是:

ping -c 1 google.com  | grep "bytes from" | cut -d = -f 2

获得与0 ttl相同的输出是字段#2

答案 1 :(得分:1)

ping -c 1 google.com | grep "bytes from" | cut -d = -f2
0 ttl

如果你希望到0 ttl那么

ping -c 1 google.com | grep "bytes from" | cut -d = -f1-2
64 bytes from yyz08s14-in-f14.1e100.net (172.217.2.142): icmp_seq=0 ttl