File 1:
JavaScript
x
a
a
b
c
d
File 2:
JavaScript
a a1
b b1
e e1
f f1
My desired output:
JavaScript
a a1
a a1
b b1
I am trying to implement this using bash or Python.
In python I tried:
JavaScript
f1=open("file1")
f2=open("file2")
dpo1=f1.readlines()
dpo2=f2.readlines()
for i in dpo2:
for j in dpo1:
if j in i:
print i
In bash I was thinking of using grep but grep will give the output that matches the entire line but this is not the case here. Any ideas?
Advertisement
Answer
This is what works finally.
JavaScript
awk 'NR==FNR{a[$1]=$2;next}$0 in a{print $0,a[$0]}' file2 file1