Solved

Comparing lines in two files

Posted on 2007-11-13
9
183 Views
Last Modified: 2010-03-05
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
Comment
Question by:David Aldridge
  • 3
  • 3
  • 2
  • +1
9 Comments
 
LVL 39

Accepted Solution

by:
Adam314 earned 500 total points
ID: 20275766
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
 
LVL 84

Expert Comment

by:ozo
ID: 20275846
perl -lne 'print "$_ missing from $f" if !$h{$_}++ && $f;$f=$ARGV if eof' list1.txt list2.txt
0
 
LVL 84

Expert Comment

by:ozo
ID: 20275861
> 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
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
LVL 48

Expert Comment

by:Tintin
ID: 20275867
Assuming they are in sorted order and assuming you have the comm command, you can simply do

comm -3 list1.txt list2.txt
0
 
LVL 1

Author Comment

by:David Aldridge
ID: 20275950
I don't have the comm command and they aren't always  sorted.. I wish they were.
0
 
LVL 1

Author Comment

by:David Aldridge
ID: 20275961
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
 
LVL 84

Expert Comment

by:ozo
ID: 20275989
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
 
LVL 1

Author Comment

by:David Aldridge
ID: 20276023
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
 
LVL 39

Expert Comment

by:Adam314
ID: 20276094
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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Email validation in proper way is  very important validation required in any web pages. This code is self explainable except that Regular Expression which I used for pattern matching. I originally published as a thread on my website : http://www…
In the distant past (last year) I hacked together a little toy that would allow a couple of Manager types to query, preview, and extract data from a number of MongoDB instances, to their tool of choice: Excel (http://dilbert.com/strips/comic/2007-08…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…

830 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question