Hello,

I am trying to create a script that will compare two identical pieces of hardwares configuration files (that are added via a GUI provided as part of the device) and to synchronize the two devices configurations.

I currently have a shell script that connects to and downloads the configuration to the two devices and am struggling with the method in which to "merge" the two documents. I cannot have the configuration file completely replaced on each sync as that will wipe out historical data.

Shell script:

#!/bin/sh

# Script uses maint_dsa key for authentication.

cp ~/equinix-shaper.txt ~/equinix-shaper.txt.backup
cp ~/qwest-shaper.txt ~/qwest-shaper.txt.backup

echo Fetching Equinix Simple Queue
ssh -i ~/.ssh/maint_dsa admin-ssh@xxx.xx.xxx.x "queue simple export" > equinix-shaper.txt && sleep 5
echo Fetching Qwest Queue
ssh admin-ssh@yyy.yy.yyy.yy "queue simple export" > qwest-shaper.txt

Perl Script for Comparing - not working quite as I would like:

#! /usr/bin/env perl
use strict;
use warnings;

my $f1 = '/Users/anrima/equinix-shaper.txt';
my $f2 = '/Users/anrima/qwest-shaper.txt';
my $outfile = '/Users/anrima/shaper-difference.txt';
my %results = ();

open FILE1, "$f1" or die "File: $! does not exist \n";
while(my $line = <FILE1>){
   $results{$line}=1;
}
close(FILE1);

open FILE2, "$f2" or die "File: $! does not exist \n";
while(my $line =<FILE2>) {
   $results{$line}++;
}
close(FILE2);


open (OUTFILE, ">$outfile") or die "$outfile does not exist \n";
foreach my $line (keys %results) {
   print OUTFILE $line if $results{$line} == 1;
}
close OUTFILE;

Could I maybe get a little help as I am new to programming in general and perl in particular (requirement of the job is in perl).

Here is my code to do what you'd like:

file1.txt

1
2
3
4
5
6
7
8
9
10

file2.txt

1
2
3
4
5
7
8
9
10
10

The code:

#! /usr/bin/env perl
use strict;
use warnings;
 
my $f1 = 'file1.txt';
my $f2 = 'file2.txt';
my $outfile = 'difference.txt';
#my $f1 = '/Users/anrima/equinix-shaper.txt'; #commented out your paths
#my $f2 = '/Users/anrima/qwest-shaper.txt';
#my $outfile = '/Users/anrima/shaper-difference.txt';
my %results = ();
 
open FILE1, "<$f1" or die "File: $! does not exist \n";
my @f1=<FILE1>;
close(FILE1);
 
open FILE2, "<$f2" or die "File: $! does not exist \n";
my @f2=<FILE2>;
close(FILE2);
 
open (OUTFILE, ">$outfile") or die "$outfile does not exist \n";
my $x=0;
for (@f1){
	chomp;
	
	$_="" if(!$_);
	$f2[$x]="" if(!$f2[$x]);
	$f2[$x]=~s/[\r|\n]//sg;
	if($_ ne $f2[$x]){
		print OUTFILE "line $x\tfile1: $_\tfile2: $f2[$x]\n";
	}
	$x++;
	
}
close OUTFILE;

Output:

line 5	file1: 6	file2: 7
line 6	file1: 7	file2: 8
line 7	file1: 8	file2: 9
line 8	file1: 9	file2: 10

Edit: you might want to print $x+1 to the output file since the array starts with zero.

When I attempt to run the code I get an

exception_handler::die in test.pl at line 13

As I said I am rather new to programming and am reading about how to troubleshoot this.

Did you remove the # from the lines containing the paths and names of your files? Line 13 is trying to open the file name stored in $f1. Mike commented out the paths to your files so the script would run on his computer.

Hrm - I guess it was just something about how I entered the paths (I modified his entry to suit my needs - but I think I just got the path wrong).

It's now working as advertised - I need to try and press my knowledge to make it does what I am looking for.

Thank you and I'll keep updating as I go!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.