我怎么可以比较3个文件一起(看看有什么共同点它们之间)? [英] How can I compare 3 files together (to see what is in common between them)?

查看:131
本文介绍了我怎么可以比较3个文件一起(看看有什么共同点它们之间)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要3个文件放在一起比较,看看有多少的信息中的文件是相同的。文件格式是这样的:

I want to compare 3 files together to see how much of the information in the files are the same. The file format is something like this:

Chr11   447     .       A       C       74      .       DP=22;AF1=1;CI95=1,1;DP4=0,0,9,8;MQ=15;FQ=-78   GT:PL:GQ        1/1:107,51,0:99
Chr10   449     .       G       C       35      .       DP=26;AF1=0.5;CI95=0.5,0.5;DP4=5,0,7,8;MQ=20;FQ=11.3;PV4=0.055,0.0083,0.028,1   GT:PL:GQ        0/1:65,0,38:41
Chr12   517     .       G       A       222     .       DP=122;AF1=1;CI95=1,1;DP4=0,0,77,40;MQ=23;FQ=-282       GT:PL:GQ        1/1:255,255,0:99
Chr10   761     .       G       A       41      .       DP=93;AF1=0.5;CI95=0.5,0.5;DP4=11,34,6,35;MQ=19;FQ=44;PV4=0.29,1.8e-35,1,1      GT:PL:GQ        0/1:71,0,116:74

我只在前两列感兴趣(如果前两列是一样的话,我认为它是相等的)。这是我用比较两个文件的COMAND:

I'm only interested in the first two columns (if the first two columns are the same then I consider it as equal). This is the comand that I use for comparing two files :

awk 'FILENAME==ARGV[1] {pair[$1 " " $2]; next} ($1 " " $2 in pair)'  file1 file2 | wc -l

我想用awk命令,因为我的文件是真正的大和awk处理它们真的很不错!但我无法弄清楚如何使用它的3个文件!

I would like to use the awk command since my files are really big and awk handle them really good! but I couldn't figure out how to use it for 3 files!

推荐答案

如果它只是打印出来,在所有3个文件是常见的对(列1 +列2),并利用这样一个事实:对是独特的内一个文件,你可以做到这一点是这样的:

If it's simply to print out the pairs (column1 + column2) that are common in all 3 files, and making use of the fact that a pair is unique within a file, you could do it this way:

awk '{print $1" "$2}' a b c | sort | uniq -c | awk '{if ($1==3){print $2" "$3}}'

这可以用文件的任意数目进行,只要您修改了最后一个命令的参数。

This can be made with arbitrary numbers of files as long as you modify the param of the last command.

下面是它做什么:


  1. 打印和排序的所有文件的第2列(的awk'{打印$ 1$ 2}'A B C |排序

  2. 计数的重复条目数(的uniq -c

  3. 如果重复条目数==文件的数量,我们找到了一个匹配。打印出来。

如果你经常这样做,你可以前preSS它作为一个bash功能(拖放在你的的.bashrc ),这parametrises文件计数

If you're doing this often, you can express it as a bash function (and drop it in your .bashrc) which parametrises the file counts.

function common_pairs { 
    awk '{print $1" "$2}' $@ | sort | uniq -c | awk -v numf=$# '{if ($1==numf){print $2" "$3}}'; 
}

你想要的任何数量的文件调用它: common_pairs文件1文件2文件3 fileN

这篇关于我怎么可以比较3个文件一起(看看有什么共同点它们之间)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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