LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Linux > Linux - Newbie
User Name
Password
Linux - Newbie This forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices

Reply
 
Thread Tools
Old 06-30-2008, 06:17 AM   #1
irfanb146
LQ Newbie
 
Registered: Mar 2008
Posts: 19
Thanked: 0
Comparing two csv files and write different record in third CSV file


[Log in to get rid of this advertisement]
How to write script for Comparing two csv file in Linux OS, and write the different record in third file.

Last edited by irfanb146; 06-30-2008 at 06:18 AM..
irfanb146 is offline     Reply With Quote
Old 06-30-2008, 08:10 AM   #2
BashTin
Member
 
Registered: May 2003
Location: West Midlands
Distribution: Slackware 11
Posts: 166
Thanked: 0
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.
BashTin is offline     Reply With Quote
Old 06-30-2008, 08:56 AM   #3
irfanb146
LQ Newbie
 
Registered: Mar 2008
Posts: 19
Thanked: 0

Original Poster
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;
irfanb146 is offline     Reply With Quote
Old 06-30-2008, 10:15 PM   #4
chrism01
Guru
 
Registered: Aug 2004
Location: Brisbane
Distribution: Centos 5.4
Posts: 7,429
Thanked: 325
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.
chrism01 is offline     Reply With Quote

Reply

Bookmarks


Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
CSV file merge help lmedland Programming 2 06-04-2008 12:11 PM
Append all CSV files in a directory into one large file br8kwall Programming 2 04-19-2008 08:44 AM
help editing .csv files schneidz Programming 7 06-11-2005 05:09 PM
convert CSV (TEXT) files to UTF-16 cccc Programming 1 07-01-2004 02:54 AM
CSV File AMMullan Programming 2 11-10-2003 01:49 AM


All times are GMT -5. The time now is 04:59 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
RSS2  LQ Podcast
RSS2  LQ Radio
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: @linuxquestions
Open Source Consulting | Domain Registration