Reputation: 454
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
Upvotes: 0
Views: 2002
Reputation: 85895
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
Upvotes: 2
Reputation: 43109
This will do it - it is pure bash and doesn't fork any sub-processes:
#!/usr/bin/env bash
# the following loop will end when an EOF is encountered on
# either of the two input files, whichever happens first.
while IFS=, read -r m1 m2 m3 <&3 && IFS=, read -r n1 n2 n3 <&4; do
if [[ $m1 == $n1 ]]; then
printf "%d %d %d\n" $m1 $((m2 - m3)) $((n2 - n3))
fi
done 3< file1.txt 4< file2.txt
Output:
12 -2 -1
13 -2 -3
Upvotes: 1
Reputation: 37464
In awk. If $1
is not yet in array a
, subtract and store the value to the array. If it already is, print the key, value in a
, and the value of subtraction. Space before 13
was already in your data:
$ awk -F, '!($1 in a){ a[$1]=$2-$3; next } { $3=$2-$3; $2=a[$1] } 1' f1 f2
12 -2 -1
13 -2 -3
Upvotes: 0
Reputation: 1770
One way to do it is using paste
and awk
-
paste file1 file2 | awk '{gsub(",", " " , $0)} {if ($1 == $4) print $1, $2-$3, $5-$6}'
Output-
12 -2 -1
13 -2 -3
Explanation-
The paste
command pastes the files side by side. The awk
gsub
command changes the commas to spaces. The second brace in the awk
checks if the first and fourth columns are the same and if they are, does the required subtraction.
Upvotes: 0