Let me give the example text files :
e.g. in file1.txt The data is:-
qwer
asdf
zxcv
1234
in file2.txt The data is:-
0987
5678
uiop
qwer
zxcv
Desired result:
qwer
zxcv
How do I get this result? I want to use python
or gawk
or sed
.
Let me give the example text files :
e.g. in file1.txt The data is:-
qwer
asdf
zxcv
1234
in file2.txt The data is:-
0987
5678
uiop
qwer
zxcv
Desired result:
qwer
zxcv
How do I get this result? I want to use python
or gawk
or sed
.
I'd use Python sets for this:
file1 = set(line.strip() for line in open('file1.txt'))
file2 = set(line.strip() for line in open('file2.txt'))
for line in file1 & file2:
if line:
print line
Use comm:
comm -12 <(sort file1.txt) <(sort file2.txt)
With gawk:
gawk 'NR==FNR {file1[$0]++; next} $0 in file1' file1.txt file2.txt