![]() |
|
|
|
|
|||||||
| Forums | Portal | Register | Forum Rules | FAQ | Contribute | Members List | Arcade | Search | Today's Posts | Mark Forums Read |
| UNIX for Advanced & Expert Users Advanced UNIX and Linux questions go here. Expert-to-Expert. |
|
|
||||
| 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 |
|
|
Submit Tools | LinkBack | Thread Tools | Search this Thread | Display Modes |
|
#15
|
|||
|
|||
|
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],
Code:
# ./test.py mary role1 role3 anne role4 jane role2 role5 sam role1 role3 role4 Code:
# ./test.py mary role2 anne role2 |
| Forum Sponsor | ||
|
|
|
#16
|
|||
|
|||
|
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... |
|
#17
|
|||
|
|||
|
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
|
|
#18
|
|||
|
|||
|
Quote:
|
|
#19
|
|||
|
|||
|
Quote:
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 |
|
#20
|
|||
|
|||
|
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:
|
|
#21
|
|||
|
|||
|
I wonder how do you get the following output,
Quote:
but it produced output of the form Quote:
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
hopefully it should work properly now! Give it a shot! Sorry for not being up to the point in the beginning itself! |
|||
| Google The UNIX and Linux Forums |
| Thread Tools | Search this Thread |
| Display Modes | |
|
|