python - How to get all lines from one file which contain a string in another file? -


file 1:

a b c d 

file 2:

a a1 b b1 e e1 f f1 

my desired output:

a a1 a1 b b1 

i trying implement using bash or python.

in python tried:

f1=open("file1") f2=open("file2") dpo1=f1.readlines() dpo2=f2.readlines()  in dpo2:     j in dpo1:         if j in i:             print 

in bash thinking of using grep grep give output matches entire line not case here. ideas?

in awk

will work if string matches field.

awk 'fnr==nr{a[$1]++;next}{for(i=1;i<=nf;i++)if(a[$i]){print;next}}' file{1,2}  a1 b b1 

for edit

awk 'fnr==nr{a[$1]++;next}{for(i=1;i<=nf;i++)if(a[$i]){for(j=1;j<=a[$i];j++)print;next}}' file{1,2}  a1 a1 b b1 

Popular posts from this blog

c# - ODP.NET Oracle.ManagedDataAccess causes ORA-12537 network session end of file -

matlab - Compression and Decompression of ECG Signal using HUFFMAN ALGORITHM -

utf 8 - split utf-8 string into bytes in python -