1748169 Members
4264 Online
108758 Solutions
New Discussion

Re: Match two files

 
Intothenewworld
Advisor

Match two files

I have two files , the content is as below , I would like to list the lines in file2 , that the lines the file1 have , as the below , both files have aaaa , cccc , eeee , so the output is the line which have aaaa , cccc , eeee , can advise what can i do ? thx

file1
=====
aaaa
bbbb
cccc
dddd
eeee


file2
=====
aaaa 1111
cccc 2222
eeee 3333


my desired result is as below
=============================
aaaa 1111
cccc 2222
eeee 3333

2 REPLIES 2
Dennis Handly
Acclaimed Contributor

Re: Match two files

You can simply use: fgrep -f file1 file2

 

if you are worried about substrings in file1 that may match multiple lines in file2, you can add -w:

grep -w -f file1 file2

RAJD1
Valued Contributor

Re: Match two files

Hi There, you can try this with awk:

 

$ awk 'NR==FNR {a[$1]=$1;next} {print a[$1],$2}' file1 file2

 

Cheers,

Raj D.