1

what the best way to find duplicate IP from file ( I have ksh script in this script I need to write function that check for duplicate IP )

for example if IP - 192.1926.23.52 exists twice in file - config_IP.txt then need to print "duplicate IP - xxx.xxx.xxx.xxx"

  • solution can be with ksh or perl one liner , sed ,awk etc

the format of the file should be as the following:

more config_IP.txt

 # NETWORK adminstration


 192.192.23.150 192.192.23.120
 192.192.23.152 192.192.23.52      
 192.192.23.153 192.192.23.53      
 192.192.23.154 192.192.23.54      


 #NETWORK private

 192.192.23.170  192.192.23.122         
 192.192.23.172  192.192.23.126  
 192.192.23.174  192.192.23.123     
 192.192.23.177  192.192.23.121  



# NETWORK oracle internal


152.3.4.12 152.3.4.12 
152.3.4.14 152.3.4.16 


# NETWORK oracle ext

152.3.4.10 152.3.4.19 
  • 1
    What is the expected output? – Thor Jan 3 '13 at 10:53
0

Something like this (Assuming you want to check for IP against the entire file, not just against the one present in the same line):

awk '/^[0-9]/{for(i=1;i<=NF;i++)if(a[$i]++==1) print "Duplicate IP : ",$i;}' file
  • Not find duplicate IP try - this - echo "10.106.23.177 10.106.23.121" > file echo "10.106.23.177 1.1.1.1" >> file awk '/^[0-9 \.]+$/{ if(a[$0]++) print "Duplicate IP : ",$0;}' RS=" " file – yael Jan 3 '13 at 10:47
  • updated the solution.. – Guru Jan 3 '13 at 10:56
2

I'm not sure I understand the question correctly, but if you want to find duplicate IPs in a file like the one above, just put one IP per line then run it through sort and then uniq -c which will report the count of identical lines before each one:

cat config_IP.txt | sed "s/\s\s*/\n/g" | sort | uniq -c

If you want to check another IPs against the file, you can just group it with the cat:

( cat config_IP.txt; echo "12.34.56.78 90.101.121.131" ) \
    | sed "s/\s\s*/\n/g" | sort | uniq -c

You might also want to add some grep to filter out lines not containing IP addresses before you start sorting.

0

try this:

cat config_IP.txt|egrep -v '^#|^$'|awk '{if($1==$2){print "Duplicate IP - "$1}}'
  • isnt find duplicate IP , please try this echo "10.106.23.177 10.106.23.121 10.106.23.177 1.1.1.1" > file , cat file |egrep -v '^#|^$'|awk '{if($1==$2){print "Duplicate IP - "$1}}' – yael Jan 3 '13 at 10:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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