linux - 两个文件之间的交集

标签 linux

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

file1.txt
a a
a b
a a
a c

file2.txt
a a

我想在 file1 中得到下面的结果(file2 中的行在 file1 中)

a a
a a

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

最佳答案

使用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 $.

关于linux - 两个文件之间的交集,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36594025/

相关文章:

linux - 使 Bash 模块化

linux - 如何让 Jenkins 执行特定文件

c - 获取 libltdl 的模块引用计数

linux - 在处理器的特定核心上运行进程

linux - 使用 shell 脚本从字符串中提取版本号

linux - 如何在循环中连接两个带有标识符的文件?

python - 如何使用 bash 脚本重新启动在屏幕内运行的 python 脚本?

python - 删除连续的 2 行

linux - 如何判断 Linux 守护进程的状态

c# - 将 Xamarin 连接到 Virtual Box