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
 
Submit Tools LinkBack Thread Tools Search this Thread Display Modes
  #8  
Old 03-10-2007
Technorati Master
 

Join Date: Mar 2005
Location: Large scale systems...
Posts: 2,610
Oops!

I should have made it clear!

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 + 1; $i++ ) {
    $dump .= $split_arr[$i];
  }
  $fileHash{$split_arr[0]} = $dump;
}

close(FILE);

open(FILE, "<", firstfile) || die ("Unable to open firstfile. <$!>\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
Reply With Quote
Forum Sponsor
  #9  
Old 03-10-2007
Registered User
 

Join Date: Mar 2007
Location: Manila
Posts: 25
Wow! You're a genius! It worked! It will save me lots of time in doing my work.
Thanks a lot!
Reply With Quote
  #10  
Old 03-12-2007
Registered User
 

Join Date: Mar 2007
Location: Manila
Posts: 25
Quote:
Originally Posted by matrixmadhan
Oops!

I should have made it clear!

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 + 1; $i++ ) {
    $dump .= $split_arr[$i];
  }
  $fileHash{$split_arr[0]} = $dump;
}

close(FILE);

open(FILE, "<", firstfile) || die ("Unable to open firstfile. <$!>\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
matrixmadhan -
I find some minor problem on the code. It should search for the exact file pattern.

In my example below:

record1
mary MI_AP
anne MI_RC

record2
role1 MI_AP_REC
role2 MI_AP MI_RC

output of the current code:
mary role1 role2
anne role2

Is it possible that it should only search for exact word so the output will be (below) ..?

mary role2
anne role2
Reply With Quote
  #11  
Old 03-12-2007
Technorati Master
 

Join Date: Mar 2005
Location: Large scale systems...
Posts: 2,610
Quote:
if( $fileHash{$key} =~ m/$dump/ ) {
if it has to match exact string change the above to

Code:
 if( $fileHash{$key} =~ $dump ) {
Try that!
Reply With Quote
  #12  
Old 03-12-2007
Registered User
 

Join Date: Mar 2007
Location: Manila
Posts: 25
Quote:
Originally Posted by matrixmadhan
if it has to match exact string change the above to

Code:
 if( $fileHash{$key} =~ $dump ) {
Try that!
It's not working
Reply With Quote
  #13  
Old 03-12-2007
Technorati Master
 

Join Date: Mar 2005
Location: Large scale systems...
Posts: 2,610
run the below as such and let us know the results

I have modified the code

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;
  @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 ) {
    @diff_arr = split(/:/, $fileHash{$key});
    for( my $i = 0; $i <= $#diff_arr; $i++ ) {
      if( $dump =~ $diff_arr[$i] ) {
        print "$key ";
      }
    }
  }
  print "\n";
}

close(FILE);

exit 0
Reply With Quote
  #14  
Old 03-12-2007
Registered User
 

Join Date: Mar 2007
Location: Manila
Posts: 25
Yes it works!!! Thank you so much.
Reply With Quote
Google The UNIX and Linux Forums
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes




All times are GMT -7. The time now is 10:02 AM.


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

Content Relevant URLs by vBSEO 3.2.0