Compare every line of one file with every line of another file

I have two files:

file1

cat file1
A,1
B,2
C,2
D,3

      

file2

cat file2
A
A
A
B
B
C
C
D

      

Desired output

cat output
A,A,1 
A,A,1
A,A,1
B,B,2
B,B,2
C,C,2
C,C,2
D,D,3

      

As you can see, each line file1

must be matched against each line file2

, and if they match, then the line from file1

must be appended to the corresponding line in file2

. I tried join

it but it doesn't work. My guess is that the search should be recursive, but I'm not sure how to do this when there are two files involved.

Any help would be greatly appreciated.

thank

+3


source to share


2 answers


Usage awk

:



awk -F, -v OFS=, 'FNR==NR {a[$1]=$0;next} $1 in a{print $1, a[$1]}' file1 file2
A,A,1
A,A,1
A,A,1
B,B,2
B,B,2
C,C,2
C,C,2
D,D,3

      

+2


source


join -t',' -o'1.1,2.1,2.2' file2 file1

      



this line does it.

+2


source







All Articles