Solved

Comparing lines in two files

Posted on 2007-11-13
9
181 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
Comment Utility
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
Comment Utility
perl -lne 'print "$_ missing from $f" if !$h{$_}++ && $f;$f=$ARGV if eof' list1.txt list2.txt
0
 
LVL 84

Expert Comment

by:ozo
Comment Utility
> 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
 
LVL 48

Expert Comment

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

comm -3 list1.txt list2.txt
0
What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

 
LVL 1

Author Comment

by:David Aldridge
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

On Microsoft Windows, if  when you click or type the name of a .pl file, you get an error "is not recognized as an internal or external command, operable program or batch file", then this means you do not have the .pl file extension associated with …
I have been pestered over the years to produce and distribute regular data extracts, and often the request have explicitly requested the data be emailed as an Excel attachement; specifically Excel, as it appears: CSV files confuse (no Red or Green h…
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…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…

771 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now