Want to print out lines with a matching pattern from file

Hi all,

I want to search for strings in file1 that can be found in file2 and print out the whole line when matching pattern is found.

I have used the below command, but this is not working for me, because it is writing out only the matching patterns from file2, not the whole line.

fgrep -o -f file1 file2 > output

For a better understanding this is how my files are looking like

file1
b
c
e
g

file2
a 1
b 2
c 3
e 4
f 5
g 6
h 7

so I would like to have in the output
b 2
c 3
e 4
g 6

Would you have any suggestion how could I use grep/fgrep for this situations? Or maybe another command like awk or sed?Many thanks in advance for your help.

Remove -o switch

fgrep -f file1 file2 > output

Hello,

Could you please use code tags for commands and codes as per forum rules. Please try following may help you.

awk 'NR==FNR{a[$1];next} ($1 in a){print $0}'  file1 file2

Output will be as follows.

b 2
c 3
e 4
g 6

Thanks,
R. Singh