LeTadas
LeTadas

Reputation: 3556

bash get just ms from ping command

how can I get just ping response time without any prefixes etc

What I'm doing right now, but it returns time=56.7 and I need just 56.7

timeinms=$(ping -c 1 $ipaddress | grep 'time' | awk '{print $7}')
echo $timeinms

Upvotes: 1

Views: 840

Answers (1)

anubhava
anubhava

Reputation: 785128

Using the PCRE option of GNU grep:

timeinms=$(ping -c 1 $ipaddress | grep -oP 'time=\K\S+')

Here we search for time= in a line and if it is found then match is reset due to \K directive and we print text until next whitespace.

Alternative solution using sed:

timeinms=$(ping -c 1 $ipaddress | sed -nE 's/.*time=([0-9.]+).*/\1/p')

Upvotes: 1

Related Questions