Scripts – How to Write an Awk Script to Catch Ping’s Time

awkscripts

In this line:

Reply from 10.11.12.13 time=1035ms

how to write an awk script which brings out just 1035.

Best Answer

You can use for example:

ping host | awk 'BEGIN {FS="[=]|[ ]"} {print $11}'

or, better to stop ping after sending one or more packets:

ping -c 1 host | awk 'BEGIN {FS="[=]|[ ]"} NR==2 {print $11}'

or

ping -c 5 host | awk 'BEGIN {FS="[=]|[ ]"} NR>=2&&NR<=6 {print $11}'

If you refer at this string: "Reply from 10.11.12.13 time=1035ms" and not at the output of ping command, you can use:

echo "Reply from 10.11.12.13 time=1035ms" | awk 'BEGIN {FS="[=]|ms"} {print $2}'