Check host file for duplicate entries


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Check host file for duplicate entries
# 1  
Old 01-21-2008
Check host file for duplicate entries

I need a KSH script that will check a host file for duplicate IP's and/or host names and report out the errors. Anyone out there have one they would like to share?

Something like:

Hostname blahblah appears X times
IP Address xxx.xxx.xxx.xxx appears X times

TIA
# 2  
Old 01-21-2008
Hacking a previous post, something along the lines of

for i in `cat /etc/hosts | cut -d"." -f1,2 | uniq`
do
num=`cat /etc/hosts | grep $i | wc -l`
if (( $num > 1 ))
then
echo $i
fi
done

which will show you the IP address that have duplicates I think, just needs polishing off with a ''wc' command ?

- someone please confirm - digging up my script memories!

-------------------------------------------------------------------
~ stevie
Solaris SCSA (v8)
Microsoft MCP
Ubuntu & Fedora on-the-way
# 3  
Old 01-21-2008
hmmmm, maybe

uniq only picks up repeats when they are consecutive lines (I think). I need the script to check the whole file for a repeat....

<---scratching head
# 4  
Old 01-21-2008
Code:
getent hosts | awk '{ ip[$1]++  
        for ( host=2; host <= NF; host++ ) { 
            hosts[$host] ++ 
        }
    }
    END { 
        for ( add in ip ) { 
            print add, ip[add], "times" 
        } 
        for ( h in hosts ) { 
            print h, hosts[h], "times" 
        }
    }'

Obviously you can add a check for only >1 occurrences if you like.
# 5  
Old 01-21-2008
Thank you both for your help, I have it now

Last edited by ThreeDot; 01-22-2008 at 01:43 PM.. Reason: question answered
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

SFTP file check through a remote host

Hi all, posting my first time, hope not breaking posting rules with it, if yes, let me know. I'm trying to build a script to check a file in an sftp server through a remote server. The intention is to check the file in a sftp host, and if the file is found or not, it should send an email.... (4 Replies)
Discussion started by: MrShinyPants
4 Replies

2. Shell Programming and Scripting

How to check duplicate entries in file ? (Solaris-9)

Hi, There are duplicate entries in file, but uniq will not see because first field is different. How will I catch all lines, which are having duplicate IPs ? bash-2.05# cat db.file | grep 172.30.133.11 dsrq-ctrl1-prod A 172.30.133.11 e911q-db1-nxge0 A 172.30.133.11... (4 Replies)
Discussion started by: solaris_1977
4 Replies

3. Shell Programming and Scripting

Check to identify duplicate values at first column in csv file

Hello experts, I have a requirement where I have to implement two checks on a csv file: 1. Check to see if the value in first column is duplicate, if any value is duplicate script should exit. 2. Check to verify if the value at second column is between "yes" or "no", if it is anything else... (4 Replies)
Discussion started by: avikaljain
4 Replies

4. Shell Programming and Scripting

Request to check:remove entries with duplicate numbers in first row

Hi I have a file 1 xyz 456 1 xyz 456 1 xyz 456 2 abc 8459 3 gfd 657 4 ghf 658 4 ghf 658 I want the output 1 xyz 456 2 abc 8459 3 gfd 657 4 ghf 658 (3 Replies)
Discussion started by: manigrover
3 Replies

5. Shell Programming and Scripting

Counting duplicate entries in a file using awk

Hi, I have a very big (with around 1 million entries) txt file with IPv4 addresses in the standard format, i.e. a.b.c.d The file looks like 10.1.1.1 10.1.1.1 10.1.1.1 10.1.2.4 10.1.2.4 12.1.5.6 . . . . and so on.... There are duplicate/multiple entries for some IP... (3 Replies)
Discussion started by: sajal.bhatia
3 Replies

6. Shell Programming and Scripting

command to check whether the remote host is up or not

Hi, My script needs to check whether the remote host is up or not. If it is up i need to start few servers in that host or else just a notification should be sent that the remote host is down? Could someone help me out in this? Regards Arun (4 Replies)
Discussion started by: arunkumarmc
4 Replies

7. Shell Programming and Scripting

to check the Linux upgrade in the host

Hi, how to check when was the linux upgrade has been done to this host. below command show the RHEL version. cat /etc/redhat-release Red Hat Enterprise Linux AS release 3 (Taroon Update 6) i want to know when it was upgraded.. is there any command available for that ? or anywhere we... (3 Replies)
Discussion started by: mail2sant
3 Replies

8. Shell Programming and Scripting

duplicate entries /.rhosts file

Hi, I forgot how to start a new thread. :( Can somebody please guide me? I have one problem related to /.rhosts file. According to my understanding, /.rhosts file is used for "rsh". What will happen if I have duplicate entries in this file? e.g> my .rhosts file looks like wcars42g... (2 Replies)
Discussion started by: akash_mahakode
2 Replies

9. Shell Programming and Scripting

duplicate entries /.rhosts file

Hi All, I have one problem related to /.rhosts file. According to my understanding, /.rhosts file is used for "rsh". What will happen if I have duplicate entries in this file? e.g> my .rhosts file looks like Code: wcars42g wcars89j wcars42g wcars42b wcars42b Will duplicate entries... (1 Reply)
Discussion started by: akash_mahakode
1 Replies

10. Shell Programming and Scripting

Removal of Duplicate Entries from the file

I have a file which consists of 1000 entries. Out of 1000 entries i have 500 Duplicate Entires. I want to remove the first Duplicate Entry (i,e entire Line) in the File. The example of the File is shown below: 8244100010143276|MARISOL CARO||MORALES|HSD768|CARR 430 KM 1.7 ... (1 Reply)
Discussion started by: ravi_rn
1 Replies
Login or Register to Ask a Question