Reputation: 1403
i want to compare the 1st columns of two files. if there is a match print the corresponding value of the 1st and 2nd column from the 1st file to the 2nd file
file1
apple,1,2
orange,2,3
pine,3,4
file2
apple,2,2
orange,4,5
Desired output
apple,2,2,1,2
orange,4,5,2,3
i was able to find the matches by awk -F, 'NR==FNR{_1[$1]++;next}_1[$1]' file2 file1
, but dont know how to append the file1 values into file2
Upvotes: 0
Views: 383
Reputation: 207405
You could use join
:
join -t "," file2 file1
Or extend your awk
:
awk -F, 'NR==FNR{_1[$1]++;_2[$1]=FS $2 FS $3;next}_1[$1]{print $0 _2[$1]}' file2 file1
Upvotes: 2
Reputation: 195039
how about :
join -t',' file2 file1
if you love to do it with awk:
awk -F',' 'NR==FNR{a[$1]=$0;sub(/^[^,]*,/,"",a[$1]);next}$1 in a{print $0 FS a[$1]}' f1 f2
Upvotes: 2