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
  #15  
Old 03-13-2007
Registered User
 

Join Date: Sep 2006
Posts: 1,580
Here's a Python alternative:
Code:
for line1 in open("file1"):
        line1 = line1.strip().split(" ",1)
        f1col = line1[1:][0].split()
        print
        print line1[0],
        for line2 in open("file2"):
                count =0
                line2 = line2.strip().split(" " ,1)
                for item1 in f1col :
                        for item2 in line2[1:][0].split():
                                if item1 == item2 : count+=1
                if count == len(f1col): print line2[0],
output:
Code:
# ./test.py

mary role1 role3
anne role4
jane role2 role5
sam role1 role3 role4
and
Code:
# ./test.py

mary role2
anne role2
Reply With Quote
Forum Sponsor
  #16  
Old 03-13-2007
Registered User
 

Join Date: Mar 2007
Location: Manila
Posts: 25
What if I want to compare the files the other way around? I can reverse the two files but it will give too many fields output based from my current file and i still have to further arrange the data to get my desired output (almost 23,000 rows for file1 record). It should be all values from file2 that is present in file1. (previous request was for all values of files in file2). Both were needed to get the desired output for my records.

File1
mary a b c d
anne e f g h
jane a d e
sam g h

File2
role1 a b
role2 a b c
role3 g h
role4 a e
role5 e f g

Output
mary role1 role2
anne role3 role5
jane role4
sam role3

Would appreciate it if the code is in korn or perl. Thanks in advance...
Reply With Quote
  #17  
Old 03-13-2007
Technorati Master
 

Join Date: Mar 2005
Location: Large scale systems...
Posts: 2,610
tested and it works fine!

Try this!

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 )
    }
    print "$key " if( $set == 1 )
  }
  print "\n";
}

close(FILE);

exit 0
Reply With Quote
  #18  
Old 03-13-2007
Registered User
 

Join Date: Mar 2007
Posts: 17
Quote:
Originally Posted by matrixmadhan
tested and it works fine!

Try this!

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 )
    }
    print "$key " if( $set == 1 )
  }
  print "\n";
}

close(FILE);

exit 0
This one works. Tested
Reply With Quote
  #19  
Old 03-13-2007
Registered User
 

Join Date: Mar 2007
Posts: 17
Quote:
Originally Posted by matrixmadhan
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
I found some problem with this one (the reverse of the other). The output repeats.
File1
mary MI_AP MI_RC
anne MI_RC

File2
role1 MI_AP_REC
role2 MI_AP MI_RC

Output of this code:
mary role2 role2
anne role2

Needed output:
mary role2
anne role2

Am i giving you too much problem ? This one (perl) is really new to me.
Reply With Quote
  #20  
Old 03-13-2007
Registered User
 

Join Date: Mar 2007
Location: Manila
Posts: 25
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);
Quote:
Originally Posted by The One
I found some problem with this one (the reverse of the other). The output repeats.
File1
mary MI_AP MI_RC
anne MI_RC

File2
role1 MI_AP_REC
role2 MI_AP MI_RC

Output of this code:
mary role2 role2
anne role2

Needed output:
mary role2
anne role2

Am i giving you too much problem ? This one (perl) is really new to me.
Sorry bout the confusing username above, i forgot my friend was logged in in my PC and i forgot to change user before replying..
Reply With Quote
  #21  
Old 03-13-2007
Technorati Master
 

Join Date: Mar 2005
Location: Large scale systems...
Posts: 2,610
I wonder how do you get the following output,

Quote:
Output of this code:
mary role2 role2
anne role2
Well I accept, the script had bug in it,

but it produced output of the form

Quote:
mary role2
anne
I had fixed it now,

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
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!
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 09:49 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