-1

file1

A   P
B   Q
A   Q
C   R
C   P
D   S
E   T

file2

A   X1
C   X2
Q   X3
R   X4
T   X5

output file

A   X1  P
B       Q   X3    
A   X1  Q   X3
C   X2  R   X4
C   X2  P   
D       S   
E       T   X5
  • You could try join but this is a tricky use case. It's trivially easy with SQL, though. If you can use SQL for this you should. – Wildcard Oct 12 '17 at 4:48
2
$ cat file1
A   P
B   Q
A   Q
C   R
C   P
D   S
E   T
$ cat file2
A   X1
C   X2
Q   X3
R   X4
T   X5
$ awk -v OFS='\t' 'FNR==NR{a[$1]=$2;next} {print $1, a[$1], $2, a[$2]}' file2 file1
A   X1  P   
B       Q   X3
A   X1  Q   X3
C   X2  R   X4
C   X2  P   
D       S   
E       T   X5
$ 

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.