Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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 
share|improve this question
What is the expected output? – Thor Jan 3 '13 at 10:53
up vote 0 down vote accepted

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
share|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.

share|improve this answer

try this:

cat config_IP.txt|egrep -v '^#|^$'|awk '{if($1==$2){print "Duplicate IP - "$1}}'
share|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 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.