Extracting latency value from output of ping
I am trying to pull a latency value from a ping with the following command:
ping -c 1 206.190.36.45 | awk -F" |=" '/time/{print $10"ms YH"}'
But I want to remove the digits after the .
So instead of 282.117ms US
I would like to see 282ms US
What needs to be added to the command to do so?
Solution 1:
There are probably hundred of ways to accomplish this with standard Unix tools. To list just some of them:
# extending what you have
ping -c 1 206.190.36.45 | awk -F" |=" '/time/{print $10"ms YH"}' | sed -e 's/\..*ms/ms/
# using awk only
ping -c 1 206.190.36.45 | awk -F" |=" '/time/{printf "%i%s\n", $10, "ms YH"}'
# using sed instead
ping -c 1 206.190.36.45 | sed -n 's/.*time=\(.*\)\..*/\1ms YH/p'
# a rather different approach (just for the fun of it, not really recommended)
echo "$(ping -c 1 206.190.36.45 | fgrep time= | cut -d= -f 4 | cut -d. -f 1)ms YH"