Simpler Navigation for Servers and Operating Systems - Please Update Your Bookmarks
Completed: a much simpler Servers and Operating Systems section of the Community. We combined many of the older boards, so you won't have to click through so many levels to get at the information you need. Check the consolidated boards here as many sub-forums are now single boards.
If you have bookmarked forums or discussion boards in Servers and Operating Systems, we suggest you check and update them as needed.
cancel
Showing results for 
Search instead for 
Did you mean: 

Match two files

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
Highlighted
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.