Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 188
  • Last Modified:

Comparing lines in two files

I have two lists, we'll call them list1.txt and list2.txt. They are a list of server names. I need to compare list1.txt to list2.txt and tell which servers are missing from list2.txt. I know this should be easy but I'm getting a brain lock on it. If you can just get me pointed in the right direction that would be great! The files are simple. Here are some examples.

list1.txt
-------------
S02ASERVER1
S02ASERVER2
S02ASERVER3
S02ASERVER4

list2.txt
--------------
S02ASERVER1
S02ASERVER3
S02ASERVER4
S02ASERVER5

In these examples, I want to print out a difference file that says S02ASERVE2 is missing from list1.txt.

Thanks!
David
0
David Aldridge
Asked:
David Aldridge
  • 3
  • 3
  • 2
  • +1
1 Solution
 
Adam314Commented:
my(%list1, %list2);
open(IN, "<list1.txt") or die "input: $!\n";
@list1{<IN>}=();
close(IN);

open(IN), "<list2.txt") or die "list2: $!\n";
@list2{<IN>}=();
close(IN);

foreach (keys %{{%list1, %list2}}) {
      if(!exists($list1{$_})) {print "Not in list1: $_";}
      elsif(!exists($list2{$_})) {print "Not in list2: $_";}
}

0
 
ozoCommented:
perl -lne 'print "$_ missing from $f" if !$h{$_}++ && $f;$f=$ARGV if eof' list1.txt list2.txt
0
 
ozoCommented:
> In these examples, I want to print out a difference file that says S02ASERVE2 is missing from list1.txt.
Did you mean a difference file that says S02ASERVE2 is missing from list2.txt or a difference file that says S02ASERVE5 is missing from list1.txt?
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
TintinCommented:
Assuming they are in sorted order and assuming you have the comm command, you can simply do

comm -3 list1.txt list2.txt
0
 
David AldridgeAuthor Commented:
I don't have the comm command and they aren't always  sorted.. I wish they were.
0
 
David AldridgeAuthor Commented:
Adam314, brilliant! Exaclty what I was looking for. Is there any way that I could get you to post a comment or two on that code? I suck at hashes and am having a little trouble following the logic.

0
 
ozoCommented:
If you meant both
perl  -lne 'BEGIN{%n=(@ARGV,reverse @ARGV)}push @{$h{$_}},$n{$ARGV};END{@{$h{$_}}==1&&print "$_ missing from @{$h{$_}}" for keys %h}' list1.txt list2.txt
0
 
David AldridgeAuthor Commented:
ozo, you are an absolute GENIUS, and while I am certain that your code does exactly what I want it to, I have NO idea what it's doing or how it works.  :-)  One of these days maybe I will...
0
 
Adam314Commented:
If it's possible to do it in one line, ozo will figure it out!


Comments on my previous code:
#Define two hash variables
my(%list1, %list2);

#Open first file, or die with error message
open(IN, "<list1.txt") or die "input: $!\n";

#read entire first file, create a hash element for each line, where the key is the entire line
@list1{<IN>}=();
#Another way to do the same thing:
#    while(<IN>) {
#        $list1{$_}=undef;
#    }

#Close file
close(IN);

#Process list2 same as list1
open(IN), "<list2.txt") or die "list2: $!\n";
@list2{<IN>}=();
close(IN);

#The keys ... will get the keys that are in either list1 or list2
foreach (keys %{{%list1, %list2}}) {
    #Check if this key (line in file) doesn't exist in list 1.  If not, print a message
    if(!exists($list1{$_})) {print "Not in list1: $_";}
   
    #Check if this key (line in file) doesn't exist in list 2.  If not, print a message
    elsif(!exists($list2{$_})) {print "Not in list2: $_";}
   
    #Otherwise, this key (line in file) exists in both list1 and list2
}

0

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

  • 3
  • 3
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now