Filtering two files with uncommon column

Filtering two files with uncommon column

am 18.01.2008 10:26:33 von Madhur

I would like to know the best way of generating filter of two files
based upon the following condition

I have two files. Contents of the first file is

File 1
abc def hij
asd sss lmn
hig pqr mno


File 2

jih def asd
poi iuu wer
wer pqr jjj

I would like have the output as
Output

File1
asd sss lmn
File2
poi iuu wer

Basically I want to compare the two files based on second column. If
the second
column matches on both the files do not print anything, else if there
is no matc
h in for the second column for first file in second file then print it
under Fil
e1 header, else if there is no match for the second column for second
file in fi
rst file print it under File2 header.

Thankyou
Madhur

Re: Filtering two files with uncommon column

am 18.01.2008 13:01:46 von Josef Moellers

Madhur wrote:
> I would like to know the best way of generating filter of two files
> based upon the following condition
>=20
> I have two files. Contents of the first file is
>=20
> File 1
> abc def hij
> asd sss lmn
> hig pqr mno
>=20
>=20
> File 2
>=20
> jih def asd
> poi iuu wer
> wer pqr jjj
>=20
> I would like have the output as
> Output
>=20
> File1
> asd sss lmn
> File2
> poi iuu wer
>=20
> Basically I want to compare the two files based on second column. If
> the second
> column matches on both the files do not print anything, else if there
> is no matc
> h in for the second column for first file in second file then print it
> under Fil
> e1 header, else if there is no match for the second column for second
> file in fi
> rst file print it under File2 header.

First: divide and conquer! You actually have two problems: print all=20
lines from file1 which have no corresponding line(s) in file2 and print=20
all lines from file2 which have no corresponding line(s) in file 1.

In order to solve the problem "print all lines from fileA which have no=20
corresponding line(s) in fileB", have a look at hashes. Also, for this=20
particular problem, "split" might come in usefull.

--=20
These are my personal views and not those of Fujitsu Siemens Computers!
Josef Möllers (Pinguinpfleger bei FSC)
If failure had no penalty success would not be a prize (T. Pratchett)
Company Details: http://www.fujitsu-siemens.com/imprint.html

Re: Filtering two files with uncommon column

am 18.01.2008 13:01:47 von Tad J McClellan

Madhur wrote:
> I would like to know the best way of generating filter of two files
> based upon the following condition
>
> I have two files. Contents of the first file is
>
> File 1
> abc def hij
> asd sss lmn
> hig pqr mno
>
>
> File 2
>
> jih def asd
> poi iuu wer
> wer pqr jjj
>
> I would like have the output as
> Output
>
> File1
> asd sss lmn
> File2
> poi iuu wer
>
> Basically I want to compare the two files based on second column. If
> the second
> column matches on both the files do not print anything, else if there
> is no matc
> h in for the second column for first file in second file then print it
> under Fil
> e1 header, else if there is no match for the second column for second
> file in fi
> rst file print it under File2 header.


--------------------------------
#!/usr/bin/perl
use warnings;
use strict;

my @f1 = ( 'abc def hij', 'asd sss lmn', 'hig pqr mno' );
my @f2 = ( 'jih def asd', 'poi iuu wer', 'wer pqr jjj' );

my %lines;

foreach my $line ( @f1 ) {
my(undef, $field) = split /\s+/, $line;
$lines{$field}{f1} = $line;
}

foreach my $line ( @f2 ) {
my(undef, $field) = split /\s+/, $line;
$lines{$field}{f2} = $line;
}

print "File1\n";
foreach my $field ( keys %lines ) {
print "$lines{$field}{f1}\n" unless exists $lines{$field}{f2};
}

print "File2\n";
foreach my $field ( keys %lines ) {
print "$lines{$field}{f2}\n" unless exists $lines{$field}{f1};
}
--------------------------------


--
Tad McClellan
email: perl -le "print scalar reverse qq/moc.noitatibaher\100cmdat/"