Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

compare 2 files

Status
Not open for further replies.
Jan 18, 2005
6
US
I have 2 files that I want to compare and mail the differences. One is file1 and one is a valid-host list. I want to make sure that the the contents of file1 is in the valid-host list. If there is an entry in file1 that is not in the valid-host list, I want to email it to an address.

Can anyone help me?
 
are you interested in using unix/linux command line?
 
I am pulling what I need out of the original file using the following command:

"cut -d '|' -f 7 file1.txt | cut -d ':' -f 1 | sort | uniq -c | sort -nr"

so to answer..yes I am ok with unix/linux command line

I have a file with valid hosts. I want to make sure that there arenot any entries in file1.txt that are not in the valid host file. if there are, I want to email them to me.
 
todd,

what have you tried to date?

--Paul

cigless ...
 
I have played around with diff and started writing a perl script, but it was getting more complicated than I thought it should so I posted it here. Should be straight forward..
 
It is
Code:
open FH, "<file1";
while (<FH>) {
  $hosts{$_}=0;  #write all host to a hash
}
close FH;
open FH, "<validhosts";
while (<FH>) {
  if (exists $hosts{$_}) {
     $hosts($_)++;
  }
}
close FH;

Sort your hash, and print out the keys while the count is still 0, and these should be the hosts which don't exist in the validhosts file

HTH
--Paul

cigless ...
 
of course, samples of valid_host/file1 would help

But, assuming the 2 files are in same format,
so that we are looking for unique lines in file1:

sort valid_host valid_host file1|uniq


 
ah..that would help.

To keep it simple, there are 2 files with ip addresses.
 
there are more files in the valid list than file1. that command seems to be giving me the unique address between both lists. I am trying to get a list of addresses, which hopefully is 0, that are not in the valid list.

 
uniq as default shows the unique lines of a list - ie it removes dupes.

-all-repeated 'selected' option will change that to only ouput the duplicated lines.
 
should have responed quicker, but got it... thanks a lot...that works for me...

Really appreciate the help
 
Code:
awk '
FILENAME==ARGV[ARGC-1] {
  if ( !($0 in valid) )
    print
  next
}
{ valid[$0]=1 }
' valid1 valid2 file1
 
Code:
use strict;
use warnings;

my %hosts;
my %invalid_hosts;

open (VALID, "validhosts.txt") or die "Can't open valid host file\n";
while (<VALID>) {
    chomp;
    $hosts{$_} = 1;
}

while (<>) {
    chomp;
    $invalid_hosts{$_}++ unless exists $hosts{$_};
}

print  "Invalid host\tAttempts\n";
foreach (keys %invalid_hosts) {
    print "$_\t$invalid_hosts{$_}\n";
}
Not quite as succinct, but it does count the number of times each invalid host tried to connect. But this only works if you get rid of the uniq -c from your original extract...
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top