Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Comparing lines in two files

Posted on 2007-11-13
9
Medium Priority
?
186 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
  • 2
  • +1
9 Comments
 
LVL 39

Accepted Solution

by:
Adam314 earned 2000 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
Independent Software Vendors: 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!

 
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

Enroll in September's Course of the Month

This month’s featured course covers 16 hours of training in installation, management, and deployment of VMware vSphere virtualization environments. It's free for Premium Members, Team Accounts, and Qualified Experts!

Question has a verified solution.

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

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…
Checking the Alert Log in AWS RDS Oracle can be a pain through their user interface.  I made a script to download the Alert Log, look for errors, and email me the trace files.  In this article I'll describe what I did and share my script.
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…
Six Sigma Control Plans

721 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