Need to compare two csv files values and write into another csv file

Hi all,

Am new to scripting. So i just need your ideas to help me out. Here goes my requirement.

I have two csv files
1.csv 2.csv
abc,1.24 abc,1
def,2.13 def,1

I need to compare the first column of 1.csv with 2.csv and if matches then need to compare the second coulmnof 1.csv value with 2.csv . if it is more than the 2.csv value then print 1.csv values and second coulmn value of 2.csv and like it goes..And remember the second column of 1.csv is a float value;

the ouput should be in 3.csv

def,2.13,1

my code is goes as folows:

1st way :

for line in `cat 1.csv`
do
samp1=`echo $line|cut -d\'' -f1`;
samp2=`echo $line|cut -d\'' -f2`;
for line in `cat 2.csv`
do
samp3=`echo $line|cut -d\'' -f1`;
samp4=`echo $line|cut -d\'' -f2`;
if [ $samp1 -eq $samp3 ]
then
if [ $samp2 > $samp4 ]
then
print "$samp1",""$samp2"",""$samp4" >> 3.csv;
break;
fi
fi
done
done

--------------------------------

2nd way

Using Awk i tried :

awk -F"," 'BEGIN { {samp1=$1 ; samp2=$2} {awk 'BEGIN {FS = ","} NR == 1 {samp3=$1 ; samp4=$2} \
{if [ $samp1 -eq $samp3 ]
then
if [ $samp2 -gt $samp4 ]
then
print "$samp1",""$samp2"",""$samp3" >> 3.csv
break
fi
fi}}}'1.csv}'2.csv

Using awk in awk...am not sure..:rolleyes: can anybody help on these two ways...am really worried about this since yesterday...:confused:

ManyThanks,
Chinna..:slight_smile:

Perhaps have a look at diff.

Hi Chinna,

Oops...correct me if i m wrong...

let me confirm once again..the result that you want..

file1.csv file2.csv
abc,2 abc,1
def,3 def,2

paste -d , file1.csv file2.csv > file3.csv

...file3.csv
abc,2,1
def,3,2

..here is the small solution:

awk -F"," '{if($2 > $4) {print $0}}' file3.csv|awk -F"," '{if($1 == $3) {print $1","$2","$4}}'

Output! will be like below:

abc,2,1

Thanks
Sha