The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > UNIX for Advanced & Expert Users
Google UNIX.COM


UNIX for Advanced & Expert Users Advanced UNIX and Linux questions go here. Expert-to-Expert.

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 07:34 AM
compare two files charandevu Shell Programming and Scripting 7 03-30-2008 12:20 PM
compare two txt files space13 Shell Programming and Scripting 8 09-22-2006 06:40 AM
compare files and beyond MizzGail UNIX for Dummies Questions & Answers 2 04-25-2003 10:34 AM
compare files ingunix UNIX for Dummies Questions & Answers 3 05-24-2001 08:44 AM

Reply
 
LinkBack Thread Tools Display Modes
  #22 (permalink)  
Old 03-13-2007
Registered User
 

Join Date: Mar 2007
Location: Manila
Posts: 25
Code:
#! /opt/third-party/bin/perl

open(FILE, "<", secondfile) || die ("Unable to open secondfile. <$!>\n");

while( <FILE> ) {
  chomp;
  @split_arr = split(/ /, $_);
  my $dump;
  for( my $i = 1; $i <= $#split_arr; $i++ ) {
    $dump .= ( $split_arr[$i] . ":");
  }
  $dump =~ s/:$//;
  $fileHash{$split_arr[0]} = $dump;
}

close(FILE);

open(FILE, "<", firstfile) || die ("Unable to open firstfile. <$!>\n");

while( <FILE> ) {
  chomp;
  @first_arr = split(/ /, $_);
  print "$first_arr[0] ";
  foreach my $key ( keys %fileHash ) {
    @second_arr = split(/:/, $fileHash{$key});
    for($i = 0; $i <= $#second_arr; $i++ ) {
      $set = 0;
      for( $j = 1; $j <= $#first_arr; $j++ ) {
        if( $first_arr[$j] =~ $second_arr[$i] ) {
          $set = 1;
          last;
        }
      }
      last if( $set == 0 && $#first_arr > 1 )
    }
    print "$key " if( $set == 1 )
  }
  print "\n";
}

close(FILE);

exit 0
Quote:
Verified two sources of input,

hopefully it should work properly now!

Give it a shot!

Sorry for not being up to the point in the beginning itself!

Its working fine but got one problem. I think I mentioned it on the thread that it's looking for a file pattern not the exact word. Please see below:

File1
AAABB370 MI_AP MI_RC MI_REC_DUP VIEW
AAABB371 VIEW
AAABB372 MI_UNIT VIEW
AAABB373 REQUEST APPROVE INQUIRE
AAABB374 REQUEST APPROVE MI_AP MI_RC MI_REC VIEW

File2
role1 MI_RC MI_REC
role2 MI_AP MI_REC
role3 VIEW
role4 VIEW MI_UNIT
role5 REQUEST APPROVE INQUIRE
role6 REQUEST

Code Output:
AAABB370 role1 role3 role2
AAABB371 role3
AAABB372 role3 role4
AAABB373 role6 role5
AAABB374 role1 role3 role6 role2

Needed Output:
AAABB370 role3 role2
AAABB371 role3
AAABB372 role3 role4
AAABB373 role6 role5
AAABB374 role3 role6 role2

Last edited by kharen11; 03-13-2007 at 06:56 PM.
Reply With Quote
Forum Sponsor
  #23 (permalink)  
Old 03-13-2007
Technorati Master
 

Join Date: Mar 2005
Location: Large scale systems...
Posts: 2,448
Quote:
File1
AAABB370 MI_AP MI_RC MI_REC_DUP VIEW
AAABB371 VIEW
AAABB372 MI_UNIT VIEW
AAABB373 REQUEST APPROVE INQUIRE
AAABB374 REQUEST APPROVE MI_AP MI_RC MI_REC VIEW

File2
role1 MI_RC MI_REC
role2 MI_AP MI_REC
role3 VIEW
role4 VIEW MI_UNIT
role5 REQUEST APPROVE INQUIRE
role6 REQUEST

Code Output:
AAABB370 role1 role3 role2
AAABB371 role3
AAABB372 role3 role4
AAABB373 role6 role5
AAABB374 role1 role3 role6 role2

Needed Output:
AAABB370 role3 role2
AAABB371 role3
AAABB372 role3 role4
AAABB373 role6 role5
AAABB374 role3 role6 role2
I couldnt understand the statements really!


So, the difference is in these two,
Code:
AAABB370 role1 role3 role2
AAABB374 role1 role3 role6 role2
And if you could see that,

when you say the needed output should be
AAABB370 role3 role2

and not AAABB370 role1 role2 role3

... how is role2 matched with AAABB370 then ? ....

' role2 ' should not be displayed with AAABB370 as per your statement.

Either of the case pattern is matched MI_REC_DUP is matched with MI_REC with role2

T
Sorry, If I havent understood it correctly!

I believe am getting the output as you had said before.


If not clear, please post with clarifications !
Reply With Quote
  #24 (permalink)  
Old 03-13-2007
Registered User
 

Join Date: Mar 2007
Location: Manila
Posts: 25
Quote:
Originally Posted by matrixmadhan
I couldnt understand the statements really!


So, the difference is in these two,
Code:
AAABB370 role1 role3 role2
AAABB374 role1 role3 role6 role2
And if you could see that,

when you say the needed output should be
AAABB370 role3 role2

and not AAABB370 role1 role2 role3

... how is role2 matched with AAABB370 then ? ....

' role2 ' should not be displayed with AAABB370 as per your statement.

Either of the case pattern is matched MI_REC_DUP is matched with MI_REC with role2

T
Sorry, If I havent understood it correctly!

I believe am getting the output as you had said before.


If not clear, please post with clarifications !

Opps!! sorry..

The output should be:
AAABB370 role3 (without the role2)
AAABB371 role3
AAABB372 role3 role4
AAABB373 role6 role5
AAABB374 role1 role2 role3 role6

For clarification:
AAABB370 cannot have role1 and role2 because his role is MI_REC_DUP and not MI_REC.

AAABB374 should have role1 and role2 because it has tha exact name of MI_REC.

Hope its clearer this time..

Last edited by kharen11; 03-13-2007 at 09:42 PM.
Reply With Quote
  #25 (permalink)  
Old 03-14-2007
Technorati Master
 

Join Date: Mar 2005
Location: Large scale systems...
Posts: 2,448
This time its very clear!

Thats clearly my mistake

change the following line
from
Quote:
if( $first_arr[$j] =~ $second_arr[$i] ) {
messed up with the operator!

Code:
if( $first_arr[$j] eq $second_arr[$i] ) {
and here is the output!
Code:
AAABB370 role3
AAABB371 role3
AAABB372 role3 role4
AAABB373 role5 role6
AAABB374 role6 role1 role2 role3
and this time, i hope so it would be perfect!
Reply With Quote
  #26 (permalink)  
Old 03-14-2007
Registered User
 

Join Date: Mar 2007
Location: Manila
Posts: 25
Quote:

Change the following line
from
Code:
if( $first_arr[$j] =~ $second_arr[$i] ) {
to

Code:
if( $first_arr[$j] eq $second_arr[$i] ) {

Perfect!

That works wonders!!!! Thank you so much for the BIG help
Reply With Quote
Google UNIX.COM
Reply

Thread Tools
Display Modes




All times are GMT -7. The time now is 05:09 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008 The CEP Blog All Rights Reserved -Ad Management by RedTyger Visit The Global Fact Book

Content Relevant URLs by vBSEO 3.2.0