Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
Perl Guru….
I need to compare two diff file (file1.abc will locate in current server and file2.abc will locate in remote server), basically the script will look for match in both file and only will send out email if there is no match and also give me list of unmatch and dups as well.
So here is the logic how the script will work.
The script will reside in Server1.
Server 1 has 13 different files like:file1.abc …………..>compare with remote server (server 2) file2.abc
file1.bcd…………..>compare with remote server (server 3) file2.abc
file1.dfg …………..>compare with remote server (server 4) file2.abc
file1.ghi…………..>compare with remote server (server 5) file2.abc
file1.ijk…………..>compare with remote server (server 6) file2.abc
file1.klm…………..>compare with remote server (server 7) file2.abc
file1.mno…………..>compare with remote server (server 8) file2.abc
file1.opq…………..>compare with remote server (server 9) file2.abc
file1.qrs…………..>compare with remote server (server 10) file2.abc
file1.stu…………..>compare with remote server (server 11) file2.abc
file1.uvw …………..>compare with remote server (server 12) file2.abc
file1.wxy…………..>compare with remote server (server 13) file2.abc
file1.yza…………..>compare with remote server (server 14) file2.abc(The file names before extension are the same in Server1, but all filename are same in remote servers)
Now I need to compare file1.abc with remote server (Server 2) filename (file2.abc) (/xxx/xyx/mns/file2.abc). The script need to compare only with component name
In this example sunsoloaris, hpunix, linux.
All of the file has same format in both file types.
Format is like the following:file1.abc format:
Fsdfsdf kkldfsd
Component sunsolaris
Sdfsdfs dfdsfds
Sdfsdf sdfsdf
Sdfsdf ertyertre
Sdfsdf hfdghdfgdFsdfsdf kkldfsd
Component hpunix
Sdfsdfs dfdsfds
Sdfsdf sdfsdf
Sdfsdf ertyertre
Sdfsdf hfdghdfgdFsdfsdf kkldfsd
Component linux
Sdfsdfs dfdsfds
Sdfsdf sdfsdf
Sdfsdf ertyertre
Sdfsdf hfdghdfgd
Following is file2.abc (Remote server – server 2)Fsdfsdf kkldfsd
Component sunsolaris
Sdfsdfs dfdsfds
Sdfsdf sdfsdf
Sdfsdf ertyertre
Sdfsdf hfdghdfgdFsdfsdf kkldfsd
Component winxp
Sdfsdfs dfdsfds
Sdfsdf sdfsdf
Sdfsdf ertyertre
Sdfsdf hfdghdfgdFsdfsdf kkldfsd
Component hpunix
Sdfsdfs dfdsfds
Sdfsdf sdfsdf
Sdfsdf ertyertre
Sdfsdf hfdghdfgdFsdfsdf kkldfsd
Component winxp
Sdfsdfs dfdsfds
Sdfsdf sdfsdf
Sdfsdf ertyertre
Sdfsdf hfdghdfgd
Output will be in an email:
(If exact match of Component found don’t send email.)
If exact match of Component not found send email. E-mail should indicate:
Component linux not found in file2.abc in remote server (server 2)
Multiple entries of Component winxp found in file2.abc in remote server (server 2)To get date from remote server login and passwd will be required.
Thanks.

I'm one of the few Perl programmers here, so I guess I'm the resident "Perl Guru". I'd be happy to assist you with your script, but I'm not interested in doing the whole script for you.
Have you starting working on the script? If so, post your code and specific question(s) on the part(s) that's giving you problems.

BTW, do you also use the username ricky007? That user has almost the exact same request, which would indicate that either you are ricky007 or that you both are taking the same Perl programming class.

No, I am not ricky007. I did not find exact same request. Was it resolved?
I am thinking how do I do it. I just need some guide. Thanks.

What determines that file1.abc needs to be compared with file2.abc on server2 and not server3 or server4?
What connection method do you use between the servers; NFS mount, telnet, ftp, ssh?
1) Build an array or hash of the file names on Server1.
2) As you loop through that array/hash open the file and build a hash of the component names. The value of each hash key could be a counter.
3) Within each of those loops, access/open the remote file and increment the counter in the "component" hash for each component found in the remote file.
4) Loop through the component hash and any value that's greater than 1 tells you that it's in both files.
I've left out a lot of details, but that should get you started.

Thank you very much, I am still struggling.
Please see below what I got so far:Q. What determines that file1.abc needs to be compared with file2.abc on server2 and not server3 or server4?
What connection method do you use between the servers; NFS mount, telnet, ftp, ssh?
A. I have to hardcode file1.abc needs to be compared with file2.abc server2 and so on..... That is a direct relationship, it will not go to any other server. Connection method will be ssh.
1) Build an array or hash of the file names on Server1.#Building arrays for filenames of server1
@server1_files = (
‘file1.abc’,
‘file1.bcd’,
‘file1.dfg’,
‘file1.ghi’,
‘file1.ijk’,
‘file1.klm’,
‘file1.mno’,
‘file1.opq’,
‘file1.qrs,
‘file1.stu’,
‘file1.uvw’,
‘file1.wxy’,
‘fle1.yza’);
2) As you loop through that array/hash open the file and build a hash of the component names. The value of each hash key could be a counter.
while ( my @server1_files = <file1.*> ) {
my %components_names;
open(FILE, @server1_files) || die $!;
while (<FILE>) {
chomp;
$components_names{$_}++; # create hash
}
foreach my $components_names (keys % components_names) {
if ($components_names {$ components_names } > 1) {
print "$ server1_files duplicated values: $components_names \n";
}
}
}

Since you need to hard code the file to server relations, server1_files should be a hash instead of an array.
%server1_files = (
‘file1.abc’ => 'server2',
‘file1.bcd’ => 'server3',
‘file1.dfg’ => 'server4',
‘file1.ghi’ => 'server5',
‘file1.ijk’ => 'server6',
‘file1.klm’ => 'server7',
‘file1.mno’ => 'server8',
‘file1.opq’ => 'server9',
‘file1.qrs' => 'server10',
‘file1.stu’ => 'server11',
‘file1.uvw’ => 'server12',
‘file1.wxy’ => 'server13',
‘file1.yza’ => 'server14',
);foreach my $file ( keys %server1_files ) {
my %components;
open my $FILE, '<', $file or die $!;
while ( my line = <$FILE> ) {
chomp $line;
next unless $line =~ /Component (.*)/;
$components{$1}++;
}
close $FILE;
# I'm skipping the needed ssh code# Now, you need to open the remote file
# and do another while loop like before.
# I'd probably look into using File::Remote
# to access the other files, which handles
# the ssh stuff in the background
}
http://search.cpan.org/~nwiger/File...BTW, please wrap your code in the html pre tags so that the code indentation is preserved.

Thank you, here what I have compiled but I am confused how to combine ssh to remote server and then open the file from that remote server:
#Special :replace tag to overload Perl builtins!use File::Remote qw(:replace); # special :replace tag
# Read from a remote file located in remote server, connect to remote server via ssh login
my $secure = File::Remote->new(rsh => :’/export/home/rf/filename’;
open(REMOTE, "server:/export/home/rf/filename") or die $!;
while ( my line = <REMOTE> ) {
chomp $line;
next unless $line =~ /Component (.*)/;
$components{$1}++;
}
close (REMOTE);

Actually, I haven't yet used the File::Remote module, so I'll need to do some testing of my own. My assumption is that you'll need to setup your ssh keys correctly (i.e., putting your public key in the authorized_keys file) so that you can login without being prompted for your password.

Can I hardcode the login and passwd instead?
Also %server1_files hash I need to indicate where the files are located
(exp/usr/mtr/)?Please assist.
Thanks

No, I haven't given up on you. ;) I've been tied up on several important projects at work and haven't had much free time.
I'm not sure if the File::Remote module supports plain text password authentication, but there are other modules to choose from. It's late here, so I post again tomorrow with 1 or 2 possible options.

![]() |
![]() |
![]() |

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.
| Ads by Google |