Find duplicate value and create an

I need a perl script, which will run every midnight via cronjob and e-mail few users once it finds any duplicated value in a file which is located /etc/hosts, the file name is called hosts and the format of the file has 3 colums and some time 2 columns. The script will look for duplicate IP or Duplicate device name, the script must not ignore any row start with "#". The file format <IP>TAB<DeviceName>TAB<Description>, I really dont care the third column. Here is an example of the file:

I will appreciate your help.

I got some of the IP address part of it. Having trouble with the host part. See if this helps any:

#!/usr/bin/perl

# find duplicate entries in /etc/hosts

print "\nDUPLICATE IP ADDRESSES\n";
print "----------------------\n";

foreach (@ips=`grep -E '^[0-9]' /etc/hosts | cut -f1`) {
        chomp(@ips);
        $count = `grep -c $_ /etc/hosts`;

        if ($count > 1) {
                print "$_\n";
        }
}

I have compiled the following works fine, but I am getting IP and device all together. I need to separate them, I need only the duplicate device name and then next paragraph duplicate IP only:

#!/opt/sa/bin/perl
use strict;
use warnings;
use MIME::Lite;
my (%ip, %host, $duplicates);
my $host_file = '/etc/hosts';
open my $file, '<', $host_file or die "can't open $host_file $!";
while (<$file>) {
if( my ($ip, $host) = /^#?([\d.]+)\s+(\S+)/ ) {
if ( defined $ip{$ip} or defined $ip{$host} ) {
$duplicates .= $_;
}
else {
$ip{$ip}++;
$ip{$host}++;
}
}
}
close $file;
my $email_msg = <<EMAIL_MSG;
The following entries in the host file are dulpicates
either by IP address or by hostname.
$duplicates
EMAIL_MSG
my $email = MIME::Lite->new(
From => 'xx@xx.com',
To => 'xv@xv.com',
Cc => '33@xx.com,42@xx.com',
Subject => 'Host file duplicates',
Data => $email_msg
);
$email->send;

Out put of above script:

119.76.169.15 aaaa
129.76.169.2 bcd
#192.16.5.30 headm
#79.158.221.4 tailm

I need output like follwoing when conditions are true:

Duplcate IP found:

139.76.169.15
129.76.169.2

Duplicate host/device found

headm
tailm

Here is my final code but I need help:

  1. All duplicates IP are not going under "Duplicate IP" section. Like the folllwing example:

10.16.15.0 abcdefg0000 abcdefg0000
10.16.15.0 abcdefg0000 abcdefg0000

Should have gone to Duplicate IP, but it went under duplicate hosts.

  1. if there are duplicates of device and IP
    like follow:

#172.11.111.222 abc014i00def
#172.11.111.222 abc014i00def

the output went in both section "Duplicate IP" and "Deuplicate hosts" instead going one of the section.

  1. Output not coming up with TAB in between the IP<>DeviceName<Restoftheinfo>

use strict;
use warnings;
use MIME::Lite;
my (%ip, %host, $duplicate_ip, $duplicate_host);
my $host_file = '/etc/hosts';
open my $file, '<', $host_file or die "can't open $host_file $!";
while (<$file>) {
if( my ($ip, $host) = /^#?([\d.]+)\s+(\S+)/ ) {
push @{$ip{$ip}}, $;
push @{$host{$host}}, $
;
}
}
close $file;
#print "Duplicate IP's with hostnames\n";
foreach my $ip ( keys %ip ) {
if ( @{$ip{$ip}} > 1 ) {
$duplicate_ip .= join ('', @{$ip{$ip}}) . "\n\n";
}
}
#print "\nDuplicate hostnames with IP's\n";
foreach my $host ( keys %host ) {
if ( @{$host{$host}} > 1 ) {
$duplicate_host .= join ('', @{$host{$host}}) . "\n\n";
}
}
my $email_msg = <<EMAIL_MSG;
The following entries in the host file are duplicates
either by IP address or by hostname.
Duplicate IP addresses:
$duplicate_ip
Duplicate Hostnames:
$duplicate_host
EMAIL_MSG
print $email_msg;
my $email = MIME::Lite->new(
From => 'xxxx@xx.com',
To => 'xxxx@xx.com',
#Cc => 'yyyy@yy.com,xxzzz@zz.com',
Subject => 'Host file duplicates',
Data => $email_msg
);
$email->send