![]() |
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.
|
|
google unix.com
|
|||||||
| Forums | Register | Forum Rules | Links | Albums | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
| UNIX for Advanced & Expert Users Expert-to-Expert. Learn advanced UNIX, UNIX commands, Linux, Operating Systems, System Administration, Programming, Shell, Shell Scripts, Solaris, Linux, HP-UX, AIX, OS X, BSD. |
More UNIX and Linux Forum Topics You Might Find Helpful
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| compare 2 files.. | amon | Shell Programming and Scripting | 8 | 06-23-2008 10:34 AM |
| compare two files | charandevu | Shell Programming and Scripting | 7 | 03-30-2008 03:20 PM |
| compare two txt files | space13 | Shell Programming and Scripting | 8 | 09-22-2006 09:40 AM |
| compare files and beyond | MizzGail | UNIX for Dummies Questions & Answers | 2 | 04-25-2003 01:34 PM |
| compare files | ingunix | UNIX for Dummies Questions & Answers | 3 | 05-24-2001 11:44 AM |
![]() |
|
|
LinkBack | Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
|
||||
|
Compare files
Hi Masters! I know this problem is quite difficult.
I have two files that looked like this: File1 mary a b d anne d e jane g h sam a File2 role1 a d c d role2 e f g h role3 a b d role4 a d e role5 g h It will first look into file1 then compare to all entries in file2 regardless of arrangement. The output should be: mary role1 role3 anne role 4 jane role2 role5 sam role1 role3 role4 I've tried some other way to do this but unsuccessful. Can it be done in UNIX? Please help. Thanks! |
|
||||
|
It's not a homework. Actually I tried using excel to manipulate the files but I'm not familiar with macros so I hope someone can help me do it in UNIX.
This will be a repeated process for me and having a right code will really help me a lot. Unfortunately my limited knowledge in UNIX wont help me too. |
|
||||
|
The files on file1 can go over several rows and columns as well as for file2. For example, mary has a, b and d. It will then search the file2 that contains a, b and d (it should be all not any) which in this case role1 and role3. Then it will begin searching for second entry in file1 who is anne, and so on.
|
|
||||
|
Code:
#! /opt/third-party/bin/perl
open(FILE, "<", b) || die ("Unable to open file. <$!>\n");
while( <FILE> ) {
chomp;
@split_arr = split(/ /, $_);
my $dump;
for( my $i = 1; $i < $#split_arr + 1; $i++ ) {
$dump .= $split_arr[$i];
}
$fileHash{$split_arr[0]} = $dump;
}
close(FILE);
open(FILE, "<", a) || die ("Unable to open file. <$!>\n");
while( <FILE> ) {
chomp;
@split_arr = split(/ /, $_);
my $dump;
for( my $i = 1; $i < $#split_arr + 1; $i++ ) {
$dump .= $split_arr[$i];
}
print "$split_arr[0] ";
foreach my $key ( keys %fileHash ) {
if( $fileHash{$key} =~ m/$dump/ ) {
print "$key ";
}
}
print "\n";
}
close(FILE);
exit 0
from the examples provided, for mary only role3 would match and not both role3 and role1 Could you please check and confirm that ? ![]() |
|
||||
|
Quote:
Yes, you're right Can you tell me where in the code is the first file and the second file? |
![]() |
| Bookmarks |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|