awk根据特定的列值完全删除重复的行

编程入门 行业动态 更新时间:2024-10-26 20:21:46
本文介绍了awk根据特定的列值完全删除重复的行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有一个像这样的数据集:

I got a dataset like:

6 AA_A_56_30018678_E 0 30018678 P A 6 SNP_A_30018678 0 30018678 A G 6 SNP_A_30018679 0 30018679 T G 6 SNP_A_30018682 0 30018682 T G 6 SNP_A_30018695 0 30018695 G C 6 AA_A_62_30018696_Q 0 30018696 P A 6 AA_A_62_30018696_G 0 30018696 P A 6 AA_A_62_30018696_R 0 30018696 P A

如果第4列重复,我想删除所有行.

I want to remove all the rows if col 4 have duplicates.

我已经使用以下代码(使用sort,awk,uniq和join ...)来获取所需的输出,但是,有没有更好的方法呢?

I have use the below codes (using sort, awk,uniq and join...) to get the required output, however, is there a better way to do this?

sort -k4,4 example.txt | awk '{print $4}' | uniq -u > snp_sort.txt join -1 1 -2 4 snp_sort.txt example.txt | awk '{print $3,$5,$6,$1}' > uniq.txt

这是输出

SNP_A_30018679 T G 30018679 SNP_A_30018682 T G 30018682 SNP_A_30018695 G C 30018695

推荐答案

使用命令替换:首先在第四字段中仅打印unique列,然后grep这些列.

Using command substitution: First print only unique columns in fourth field and then grep those columns.

grep "$(echo "$(awk '{print $4}' inputfile.txt)" |sort |uniq -u)" inputfile.txt 6 SNP_A_30018679 0 30018679 T G 6 SNP_A_30018682 0 30018682 T G 6 SNP_A_30018695 0 30018695 G C

注意:如果希望打印前四列,请在命令末尾添加awk '{NF=4}1'.当然,您可以通过更改$4和NF=4的值来更改列数.

Note: add awk '{NF=4}1' at the end of the command, if you wist to print first four columns. Of course you can change the number of columns by changing value of $4 and NF=4.

更多推荐

awk根据特定的列值完全删除重复的行

本文发布于:2023-10-17 02:59:11,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1499576.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:awk

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!