Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Using Awk to print all output except for... 1

Status
Not open for further replies.

jdespres

MIS
Aug 4, 1999
230
US
I need to find any un-sed IP's within a specified range...

I set up a file with all of them in it: IP.txt

I then run the following:

cat IP.txt | xargs -n 1 ping -c 10

This produces the following output:

xx.xx.xx.xx is alive
xx.xx.xx.xx is alive
xx.xx.xx.xx is alive
xx.xx.xx.xx is alive
xx.xx.xx.xx is alive
no answer from xx.xx.xx.xx
no answer from xx.xx.xx.xx
xx.xx.xx.xx is alive
xx.xx.xx.xx is alive

I would like to produce the following output:

xx.xx.xx.xx is alive
xx.xx.xx.xx is alive
xx.xx.xx.xx is alive
xx.xx.xx.xx is alive
xx.xx.xx.xx is alive
xx.xx.xx.xx no answer from
xx.xx.xx.xx no answer from
xx.xx.xx.xx is alive
xx.xx.xx.xx is alive

Thanks....

Joe Despres
 
Hi

Code:
sed 's/\(.*\) \([0-9]\+\.[0-9]\+\.[0-9]\+\.[0-9]\+\)/\2 \1/'
Code:
awk '{print gensub(/(.*) ([0-9]+\.[0-9]+\.[0-9]+\.[0-9]+)/,"\\2 \\1","")}'
Code:
awk -vip='[0-9]+\\.[0-9]+\\.[0-9]+\\.[0-9]+' '$1!~ip{for(i=2;i<=NF;i++)if($i~ip){s=$i;for(j=i;j>1;j--)$j=$(j-1);$1=s}}1'

Tested with [tt]gawk[/tt] and [tt]mawk[/tt].

Feherke.
 
A quick and dirty awk solution (works even on an old AIX 4.3.3 box):

Code:
awk '
/no answer from/{
 $0=$NF" no answer from"
}
{
 print
}'

or for short:

Code:
awk '
/no answer from/{
 $0=$NF" no answer from"
}
[red]1[/red]'

HTH,

p5wizard
 
Tried the code:

cat IPs.txt | xargs -n 1 ping -c 10 | sed 's/\(.*\) \([0-9]\+\.[0-9]\+\.[0-9]\+\.[0-9]\+\)/\2 \1/'
20.159.0.1 is alive
20.159.0.2 is alive
20.159.0.3 is alive
no answer from 20.159.0.4
no answer from 20.159.0.5
20.159.0.6 is alive
20.159.0.7 is alive
20.159.0.8 is alive
20.159.0.9 is alive
no answer from 20.159.0.10


cat IPs.txt | xargs -n 1 ping -c 10 | awk '{print gensub(/(.*) ([0-9]+\.[0-9]+\.[0-9]+\.[0-9]+)/,"\\2 \\1","")}'
awk: syntax error near line 1
awk: illegal statement near line 1
xargs: Child killed with signal 13

cat IPs.txt | xargs -n 1 ping -c 10 | awk -vip='[0-9]+\\.[0-9]+\\.[0-9]+\\.[0-9]+' '$1!~ip{for(i=2;i<=NF;i++)if($i~ip){s=$i;for(j=i;j>1;j--)$j=$(j-1);$1=s}}1'
awk: syntax error near line 1
awk: bailing out near line 1
xargs: Child killed with signal 13

all 3 didn't work....
 
Tried the 2nd set:

cat IPs.txt | xargs -n 1 ping -c 10 |awk '/no answer from/{$0=$NF" no answer from"}{ print }'
20.159.0.1 is alive
20.159.0.2 is alive
20.159.0.3 is alive
awk: can't set $0
record number 4
xargs: Child killed with signal 13

 
Either use nawk or try this :
Code:
cat IPs.txt | xargs -n 1 ping -c 10 |awk '{x=$0}/no answer from/{x=$NF" no answer from"}{print x}'

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
Using nawk on that last one did it!

Thanks PHV.....

Joe Despres
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top