我是编程新手,我正在尝试找到两个文本文件的交集。
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/