【问题标题】:find the intersection of 4 files找到 4 个文件的交集
【发布时间】:2017-03-06 09:12:59
【问题描述】:

如何找到 4 个文件的交集?

我使用了grep -Fx -f 1.txt 2.txt 3.txt 4.txt,但它似乎只适用于 2 个文件。同样comm -12 1.txt 2.txt 不能扩展为 4 个文件,需要对数据进行排序。我的数据没有排序,事实上,它不应该排序。我在 Mac 上。

输入:

1.txt
.css
.haha
.hehe
2.txt
.ajkad
.shdja
.hehe

3.txt
.css1
.aaaaa
.hehe

4.txt
.qwqw
.kkkmsm
.hehe

输出:.hehe

我首先在 1 和 2 上使用 grep,然后在 3 和 4 上存储在 34.txt 中,然后在 12.txt 和 34.txt 中再次使用 grep。但是必须有更好的方法来做到这一点。

【问题讨论】:

  • 这种情况下的交集是什么意思?你能提供一个简短的输入示例和所需的输出吗?
  • 你去...

标签: macos ubuntu terminal string-search


【解决方案1】:

您可以链接 greps:

grep -f 1.txt 2.txt \
| grep -f- 3.txt \
| grep -f- 4.txt

-f- 表示使用来自管道的输入,而不是真实文件。

【讨论】:

  • @sanjihan:grep -f <(grep -f <(grep -f 1.txt 2.txt) 3.txt) 4.txt 呢?
  • 这是正确的,我误解了我的数据。谢谢!
【解决方案2】:

你可以找到两者的交集:

$ grep -Fx -f f1.txt f2.txt
.hehe

然后将其用作其他两个的输入:

$ grep -f <(grep -Fx -f f1.txt f2.txt) f3.txt f4.txt
f3.txt:.hehe
f4.txt:.hehe

或者,如果你只想要一个词:

$ grep -f <( grep -f <(grep -Fx -f f1.txt f2.txt) f3.txt) f4.txt
.hehe

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-08-27
    • 2013-07-25
    • 2014-03-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多