LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (http://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Comparing two csv files and write different record in third CSV file (http://www.linuxquestions.org/questions/linux-newbie-8/comparing-two-csv-files-and-write-different-record-in-third-csv-file-652534/)

irfanb146 06-30-2008 05:17 AM

Comparing two csv files and write different record in third CSV file
 
How to write script for Comparing two csv file in Linux OS, and write the different record in third file.

BashTin 06-30-2008 07:10 AM

Don't know the exact answer to your question but take a look at the command 'diff', 'man diff'. Should be all you need with a bit of redirection I would have thought.

BashTin.

irfanb146 06-30-2008 07:56 AM

I got the answer following is the perl script from internet, to compare file 1 and file 2 and saving result to file3



$f1 = '/u01/oracle/file1.csv';
open FILE1, "$f1" or die "Could not open file file2.csv\n";
$f2= '/u01/oracle/file2.csv';
open FILE2, "$f2" or die "Could not open file2.csv\n";
$outfile = '/u01/oracle/file3.csv';
my @outlines;
foreach (<FILE1>) {
$y = 0;
$outer_text = $_;
seek(FILE2,0,0);
foreach (<FILE2>) {
$inner_text = $_;
if($outer_text eq $inner_text) {
$y = 1;
print "Match Found \n";
last;
}
}

if($y != 1) {
print "No Match Found \n";
push(@outlines, $outer_text);
}
}

open (OUTFILE, ">$outfile") or die "Cannot open $outfile for writing \n";
print OUTFILE @outlines;
close OUTFILE;
close FILE1;
close FILE2;

chrism01 06-30-2008 09:15 PM

Just FYI, its a good idea to know why a file failed to open(), so use this

open FILE1, "$f1" or die "Could not open file file2.csv: $!\n";

'$!' contains the error text.


All times are GMT -5. The time now is 03:20 PM.