两个文件之间的交集

Bar*_*rry 4 linux

我是编程新手,我正在尝试找到两个文本文件的交集。

file1.txt
a a
a b
a a
a c

file2.txt
a a
Run Code Online (Sandbox Code Playgroud)

我想得到下面的结果file1(其中的file2file1

a a
a a
Run Code Online (Sandbox Code Playgroud)

我尝试grep在 Linux 中使用命令,但我不知道该怎么做。

Jul*_*pez 5

使用grep

grep -xFf file2.txt file1.txt

-F, --fixed-strings
          Interpret  PATTERN  as  a  list  of  fixed  strings (rather than
          regular expressions), separated by newlines, any of which is  to
          be matched.
-f FILE, --file=FILE
          Obtain patterns  from  FILE,  one  per  line.   The  empty  file
          contains zero patterns, and therefore matches nothing.
-x, --line-regexp
          Select only those matches that exactly  match  the  whole  line.
          This option has the same effect as anchoring the expression with
          ^ and $.
Run Code Online (Sandbox Code Playgroud)