Sign up ×
Ask Different is a question and answer site for power users of Apple hardware and software. It's 100% free, no registration required.

I am using the following script to cycle through a list of IP addresses and report back on the % of packet loss.

I'd also like to have the IP address printed first, then its % of packet loss. But I can't get the syntax right to make this work.

Ideally, it'd look like:  25%  0%    

Here's my script:

for myHost in $HOSTS

    ping -q -n -s $SIZE -c $COUNT $myHost |  grep "packet loss" | awk '{print $7}'

share|improve this question

1 Answer 1


ping -q -n -s $SIZE -c $COUNT $myHost |
    awk -v host=$myhost '/packet loss/ {print host, $7}'

inside the loop.

In case you only want to print the hosts with packet loss use

ping -q -n -s $SIZE -c $COUNT $myHost |
    awk -v host=$myhost '/packet loss/ {if ($7 != "0.0%") print host, $7}'

Side note: grep pattern | awk '{action}' can usually be replaced with the much neater (and slightly faster) awk '/pattern/ {action}'

share|improve this answer
Perfect! Thanks Patrix! I really appreciate it. – IT Dude Aug 23 '12 at 23:00
you can simplify the quoting a bit: awk -v host=$myhost '/packet loss/ {print host, $7}' – glenn jackman Aug 23 '12 at 23:42
@glennjackman good point, didn't think about that. – patrix Aug 24 '12 at 3:22
Since this is such a handy dandy script now, how could I add an if/then clause into it that only echoes the output if the packet loss is anything other than 0% – IT Dude Aug 24 '12 at 20:02
@ITDude: just precede the print with if($7 != "0%") – Thor Aug 25 '12 at 7:49

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.