找到两个文件之间的相似性 [英] Find the similarities between two files

查看:117
本文介绍了找到两个文件之间的相似性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个文件,​​如:

I have two file like:

1
2
3
4
5
6
7
8
9
10

和两列的文件

1 0.11
2 0.12748
5 0.45
12 0.48
7 0.48
8 0.7
13 0.78
14 0.88

我想找到两个文件之间thesimilarities。如果在这两个文件1冲击片雷管也文件2的第一列中的文件数,然后就意味着有它必须出现在输出文件中。输出应该是:

I would like to find thesimilarities between the two files. If the in both files the number in file 1 apper also in the first column of file 2 then it means that there it must appear in the output file. The output should be:

1 0.11
2 0.12748
5 0.45
7 0.48
8 0.7

我试着使用grep的不同组合,但它不能正常工作。

I tried with different combination of grep but it does not work.

推荐答案

通过awk的:

awk 'NR==FNR{a[$1]++;next}$1 in a' test.1 test.2

注意在阵列中出现的第一个文件中的行,然后打印线在分别在第一,第二。线将出现在它们出现在第二个文件的顺序输出。

Note in an array the lines appearing in the first file, then print lines in the second that were in the first. Lines will appear in the output in the order they appear in the second file.

或者加入:

join <(sort test.1) <(sort test.2)

的文件需要被排序。线将出现在字典顺序。

The files need to be sorted. Lines will appear in dictionary order.

这篇关于找到两个文件之间的相似性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆