I want to compare first column of two different files and if there is a match, put the difference of second and third column of both files into a new file with first column as the matched entry.
Output File = Matched,Difference of c2 and c3 of file1,Difference of c2 and c3 of file 2
Example:
File 1
12,1,3
13,2,4
14,5,7
File 2
12,4,5
13,4,7
15,3,9
Desired Output File
12 -2 -1
13 -2 -3
Please tell me how may I do this. File length is varying. File1 is of length 100 and File2 is of length 20
Use awk
which is more suited for this.
awk 'BEGIN{FS=","}FNR==NR{array1[$1]=$2-$3; next}($1 in array1){array2[$1]=$2-$3}END{for (i in array2){print i,array1[i],array2[i]}}' file1 file2
12 -2 -1
13 -2 -3
If awk
is not native in Solaris
, Can you try nawk
as, put the below contents in a file called nawk_script.awk
BEGIN{FS=","}
FNR==NR{array1[$1]=$2-$3; next}($1 in array1){array2[$1]=$2-$3}
END{for (i in array2){print i,array1[i],array2[i]}}
and run it as
nawk -f nawk_script.awk file1 file2