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      

 #NETWORK private   

# NETWORK oracle internal 

# NETWORK oracle ext 
  • 1
    What is the expected output? – Thor Jan 3 '13 at 10:53

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
| improve this answer | |
  • Not find duplicate IP try - this - echo "" > file echo "" >> 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

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 "" ) \
    | 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.

| improve this answer | |

try this:

cat config_IP.txt|egrep -v '^#|^$'|awk '{if($1==$2){print "Duplicate IP - "$1}}'
| improve this answer | |
  • isnt find duplicate IP , please try this echo "" > 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.