如何在txt文件中查找单个条目? [英] How to find single entries in a txt file?

查看:200
本文介绍了如何在txt文件中查找单个条目?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个12列的txt文件。有些行重复,有些行不行。作为一个例子,我复制到我的数据的前4列。

  0 0 chr12 48548073 
0 0 chr13 80612840
2 0 chrX 4000600
2 0 chrX 31882528
3 0 chrX 3468481
4 0 chrX 31882726
4 0 chr3 75007624

根据第一列,您可以看到有些重复项除了条目'3'。
我想打印唯一的单个条目,在这种情况下为'3'。



输出将是

  3 0 chrX 3468481 

有没有用awk或perl快速的方法?我只能想到在perl中使用for循环,但是由于我有大约1.5M的条目,可能需要一些时间。

解决方案

尝试这个awk one-liner:

  awk'{a [$ 1] ++; b [$ 1] = $ 0文件


I have a txt file with 12 columns. Some lines are duplicated and some are not. As an example i copied to first 4 columns of my data.

0       0       chr12   48548073  
0       0       chr13   80612840
2       0       chrX    4000600 
2       0       chrX    31882528 
3       0       chrX    3468481 
4       0       chrX    31882726
4       0       chr3    75007624

Based on the first column, you can see that some there are duplicates except entry '3'. I would like to print the only single entries, in this case '3'.

The output will be

3       0       chrX    3468481

IS there a quick way of doing this with awk or perl? I can only think of using for loop in perl but given the fact that i have around 1.5M entries it will probably take some time.

解决方案

try this awk one-liner:

awk '{a[$1]++;b[$1]=$0}END{for(x in a)if(a[x]==1)print b[x]}' file

这篇关于如何在txt文件中查找单个条目?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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