Extract average time from ping -c

I want to extract from the ping -c 4 www.stackoverflow.com | tail -1| awk '{print $4}' ping -c 4 www.stackoverflow.com | tail -1| awk '{print $4}' ping -c 4 www.stackoverflow.com | tail -1| awk '{print $4}' average time.

 107.921/108.929/110.394/0.905 ms 

The conclusion should be: 108.929

+8
linux bash awk ping
source share
6 answers

One way is to simply add a shortcut to what you have.

 ping -c 4 www.stackoverflow.com | tail -1| awk '{print $4}' | cut -d '/' -f 2 
+26
source share

ping -c 4 www.stackoverflow.com | tail -1| awk -F '/' '{print $5}' ping -c 4 www.stackoverflow.com | tail -1| awk -F '/' '{print $5}' will work fine.

Option

"- F" is used to indicate the field separator.

+6
source share

This might work for you:

 ping -c 4 www.stackoverflow.com | sed '$!d;s|.*/\([0-9.]*\)/.*|\1|' 
+3
source share

The following solution uses only Bash (requires Bash 3):

 [[ $(ping -q -c 4 www.example.com) =~ \ =\ [^/]*/([0-9]+\.[0-9]+).*ms ]] \ && echo ${BASH_REMATCH[1]} 

For a regular expression, it is easier to read (and process) if it is stored in a variable:

 regex='= [^/]*/([0-9]+\.[0-9]+).*ms' [[ $(ping -q -c 4 www.example.com) =~ $regex ]] && echo ${BASH_REMATCH[1]} 
+3
source share

Promoting luissquall a very elegant comment on the answer:

  ping -c 4 www.stackoverflow.com | awk -F '/' 'END {print $5}' 
+2
source share

Average ping time:

 ping -w 4 -q www.duckduckgo.com | cut -d "/" -s -f5 

Options:

 -w time out 4 seconds -q quite mode -d delimiter -s skip line without delimiter -f No. of field - depends on your system - sometimes 5th, sometimes 4th 

I personally use this method:

 if [ $(ping -w 2 -q www.duckduckgo.com | cut -d "/" -s -f4 | cut -d "." -f1) -lt 20 ]; then echo "good response time" else echo "bad response time" fi 
0
source share

All Articles