LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux 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
  Search this Thread
Old 06-30-2008, 05:17 AM   #1
irfanb146
LQ Newbie
 
Registered: Mar 2008
Posts: 19

Rep: Reputation: 0
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.

Last edited by irfanb146; 06-30-2008 at 05:18 AM.
 
Old 06-30-2008, 07:10 AM   #2
BashTin
Member
 
Registered: May 2003
Location: West Midlands, United Kingdom.
Distribution: Slackware 14.1, Centos 7
Posts: 278

Rep: Reputation: 32
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.
 
Old 06-30-2008, 07:56 AM   #3
irfanb146
LQ Newbie
 
Registered: Mar 2008
Posts: 19

Original Poster
Rep: Reputation: 0
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;
 
Old 06-30-2008, 09:15 PM   #4
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,342

Rep: Reputation: 2746Reputation: 2746Reputation: 2746Reputation: 2746Reputation: 2746Reputation: 2746Reputation: 2746Reputation: 2746Reputation: 2746Reputation: 2746Reputation: 2746
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.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 11:11 AM
Append all CSV files in a directory into one large file br8kwall Programming 2 04-19-2008 07:44 AM
help editing .csv files schneidz Programming 7 06-11-2005 04:09 PM
convert CSV (TEXT) files to UTF-16 cccc Programming 1 07-01-2004 01:54 AM
CSV File AMMullan Programming 2 11-10-2003 12:49 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 09:40 PM.

Main Menu
Advertisement
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
Twitter: @linuxquestions
Open Source Consulting | Domain Registration