比较两个文件并获取相同行的输出

gar*_*ncn 10 linux bash

如何通过比较两个文本文件使用linux命令获得以下输出?谢谢.

文件1:

site110
site120
Run Code Online (Sandbox Code Playgroud)

文件2(更新):

domain1.com - site110
domain2.com - site111
domain3.com - site112
domain4.com - site113
domain5.com - site120
domain6.com - site1201
domain7.com - site1202
Run Code Online (Sandbox Code Playgroud)

输出:

domain1.com - site110
domain5.com - site120
Run Code Online (Sandbox Code Playgroud)

如果我使用:

grep -f file1 file2
Run Code Online (Sandbox Code Playgroud)

输出将是:

domain1.com - site110
domain5.com - site120
domain6.com - site1201
domain7.com - site1202
Run Code Online (Sandbox Code Playgroud)

最后两行不是我想要的.谢谢.

ire*_*ses 14

grep联机帮助页:

   -f FILE, --file=FILE
          Obtain  patterns  from  FILE,  one  per  line.   The  empty file
          contains zero patterns, and therefore matches nothing.   (-f  is
          specified by POSIX.)
Run Code Online (Sandbox Code Playgroud)

因此:

grep -f file1 file2

domain1.com - site110
domain5.com - site120
Run Code Online (Sandbox Code Playgroud)