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!

How to compare 2 files and find the difference?

Status
Not open for further replies.

gkrithiv

Programmer
Oct 21, 2007
1
US
Hello.. I have just begun to learn perl. I'm trying to compare the contents in 2 files and print the missing information.

-dnstemplate.txt- --------

<template:cisco:dns>
ip domain-name fanniemae.com
ip name-server 192.168.3.71
ip name-server 158.137.21.80
ip name-server 158.137.218.75
<end>

<template:cat:dns>
set ip dns server 192.168.3.71 primary
set ip dns server 158.137.218.75
set ip dns server 158.137.21.80
set ip dns enable
set ip dns domain fanniemae.com
<end>
----------------------------------------

-rconfig.txt-----
9acd-ra01.1192598100
version 12.2
vtp enabled
ip domain-name fanniemae.com
ip name-server 192.168.3.71
ip name-server 158.137.21.71
ip name-server 158.137.21.80
ip name-server 158.137.218.75
--------------------------------

Here's my script:
Difference.pl

#!/usr/bin/perl

open(DNS,"dnstemplate.txt");
open(RCONFIG,"rconfig.txt");

@dns=<DNS>;

@rconfig=<RCONFIG>;

my %seen; #lookup table

#build lookup table

@seen{@rconfig}=();

foreach $item(@dns)

{

push(@miss,$item) unless exists $seen{$item};
}

foreach(@miss)

{
print "Difference is $_";
}

close(DNS);

close(RCONFIG);

----------------------
This gives me the difference, but i want to compare the lines within the tags <template:cisco:dns> and <end> of dnstemplate.txt with rconfig.txt

Can you guys help me out on this one?

Thanks.

 
First of all you should better specify your goal.
From the information provided you seem only to be interested in finding whether the domain name and the server IP contained in a specific section of a template file match those provided in a configuration file.
Also you are only interested in the last piece of information contained in each line, and it's not very efficient to use the whole line to compare.
Also, when comparing two files, it is often better to read one of them in memory into a hash, and check the second one while reading line by line; this allows you, for example, to successively check multiple files against a single configuration file.
Concerning your problem of checking only a section named [tt]<template:cisco:dns>[/tt] (is this name always the same in your program, or is it going to change?), just read the file till you get that line doing nothing, then start processing and end it when you find the line [tt]<end>[/tt].
Please come back with a better description of your goals before we go to code examples.

Franco
: Online tools for structural design
: Magnetic brakes for fun rides
: Air bearing pads
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top