Free2Code
The majority of forums are now only available as archives, which means posting/editing is disabled.

The Anything and Everything forum is still open.
 
Time: 2013-05-23, 08:01am
comparing 2 files
Subject: comparing 2 files  ·  Posted: 2004-10-26, 11:05pm
Rank: Unregistered
Hi,

I am having 2 files. I have to compare these 2 files a particular
column and get the count how much is matching and how much is not matching.
For eg., first file has 500 reords and the second file has 700 reords. Both files are having around 20 fieds separated by "|".

FILE 1:
ID|Name|Address|.........|||||....
1+5e|jesica|37, cross street|......
1+63e|albert|81, church avenue|....

FILE2:
ID|Name|Address|.........|||||....
1+78r|rene|92, pearl street|..............
1+5e|jesica thame|37, cross street|......
1+63e|albert|81, church avenue|....

In the above example, I have to compare the Name between the files as always rowid is unique betwwen the 2 files. For the above, the output should be
Matching = 1 (because names of record 2 is matching exactly) and
Non match = 1 (because names of reord1 is not matching).
I tried with the below script. But I couldn't get it. Can anyone help me?

Thanks in advance,
jacob

!#/usr/bin/perl
open(FILE,"account_id.txt");
open (OUT, ">id_freq.txt");
$count = 0;
while(<FILE>)
{ @arr=split(/\n/,$_);
$first = @arr[0];
print "$first\t";
open(FILE1,"contacts.txt");
$count = 0;
while(<FILE1>)
{ @arr1=split(/\|/,$_);
$second = @arr1[1];
chomp($second);
if($first eq $second)
{print OUT "@arr1[0]\t@arr1[1]\t"; }
if(@arr1[1] eq @arr1[1]) {
print "1\n"; }
}}
 
  Reply to this ·  Post link ·  Top
Subject: Re: comparing 2 files  ·  Posted: 2004-12-20, 03:45pm
Rank: ? (171)
Member #: 11947
heres my implimentation (i'm free typing so their might be some errors, i'll fix it when i have more time if still needed):

Code:
  1. !#/usr/bin/perl
  2. open(FILE1, shift); #opens the first file, shift grabs it from the command line
  3. open(FILE2, shift); #.................second file,...
  4. @L1 = <FILE1>;
  5. $row = 0;
  6. foreach( <FILE2> ){
  7.    @first = split( $_, | ); #splits the line around |, the 'cell' seperator.
  8.    @second = split( $L1[$row],  | );
  9.    $column = 0;
  10.    foreach $fcell @first{
  11.       if(! ($fcell == $second[$column]) ){
  12.          PRINTF("INCONSISTENCY IN CELL, ROW: %d, COLUMN: %d", $row, $column);
  13.       }
  14.    column++;
  15.    }
  16.    row++;
  17. }
I"m not the greatest at perl, but i try ... hope it at least gives you some ideas

» Post edited 2004-12-20, 03:45pm by Umojan.

 
  Reply to this ·  Post link ·  Top

Pages: 1

Please login or register to post a reply.

Penguino AVR

Want to learn about robotics or microcontrollers?
Check out the Penguino AVR from our friends at
Icy Labs