gpt4 book ai didi

linux - 两个文件之间的交集

转载 作者:太空狗 更新时间:2023-10-29 11:28:55 27 4
gpt4 key购买 nike

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

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/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com