Computing.Net > Forums > Unix > Find match in two diff files at tim

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

Find match in two diff files at tim

Reply to Message Icon

Name: rayman777
Date: March 6, 2008 at 22:13:17 Pacific
OS: Solaris
CPU/Ram: SUN
Product: SUN
Comment:

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 hfdghdfgd

Fsdfsdf kkldfsd
Component hpunix
Sdfsdfs dfdsfds
Sdfsdf sdfsdf
Sdfsdf ertyertre
Sdfsdf hfdghdfgd

Fsdfsdf 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 hfdghdfgd

Fsdfsdf kkldfsd
Component winxp
Sdfsdfs dfdsfds
Sdfsdf sdfsdf
Sdfsdf ertyertre
Sdfsdf hfdghdfgd

Fsdfsdf kkldfsd
Component hpunix
Sdfsdfs dfdsfds
Sdfsdf sdfsdf
Sdfsdf ertyertre
Sdfsdf hfdghdfgd

Fsdfsdf 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.



Sponsored Link
Ads by Google

Response Number 1
Name: FishMonger
Date: March 6, 2008 at 23:41:33 Pacific
Reply:

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.


0

Response Number 2
Name: FishMonger
Date: March 6, 2008 at 23:48:35 Pacific
Reply:

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.


0

Response Number 3
Name: rayman777
Date: March 7, 2008 at 06:19:40 Pacific
Reply:

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.


0

Response Number 4
Name: FishMonger
Date: March 7, 2008 at 09:41:55 Pacific
Reply:

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.


0

Response Number 5
Name: rayman777
Date: March 7, 2008 at 13:45:52 Pacific
Reply:

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";
}
}
}


0

Related Posts

See More



Response Number 6
Name: FishMonger
Date: March 7, 2008 at 14:38:48 Pacific
Reply:

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.


0

Response Number 7
Name: rayman777
Date: March 7, 2008 at 16:03:01 Pacific
Reply:

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);



0

Response Number 8
Name: FishMonger
Date: March 7, 2008 at 18:37:51 Pacific
Reply:

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.


0

Response Number 9
Name: ricky007
Date: March 10, 2008 at 08:55:17 Pacific
Reply:

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


0

Response Number 10
Name: ricky007
Date: March 13, 2008 at 06:30:13 Pacific
Reply:

Hello Fishmonger, have you given up on me?


0

Response Number 11
Name: FishMonger
Date: March 13, 2008 at 22:58:30 Pacific
Reply:

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.


0

Response Number 12
Name: ricky007
Date: March 18, 2008 at 07:28:32 Pacific
Reply:

Instead we should use:

use Net::SCP::Expect;


0

Sponsored Link
Ads by Google
Reply to Message Icon






Post Locked

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


Go to Unix Forum Home


Sponsored links

Ads by Google


Results for: Find match in two diff files at tim

Find un-match in two files www.computing.net/answers/unix/find-unmatch-in-two-files-/8105.html

Find string in specific position www.computing.net/answers/unix/find-string-in-specific-position/5973.html

Merging two data files www.computing.net/answers/unix/merging-two-data-files/7293.html