【问题标题】:KEEP duplicated lines using grep使用 grep 保留重复的行
【发布时间】:2021-12-25 00:56:27
【问题描述】:

我正在使用 grep 在 file1 中搜索 file2 中的模式,这个 file2 有一些重复的行,我只能得到一次。如何使用 grep 保留重复的行?

file1.txt

2L  FlyBase mRNA    11009821    11011086    .   -   .   ID=transcript:FBtr0080162;Parent=gene:FBgn0032329;Name=Art8-RA;biotype=protein_coding;transcript_id=FBtr0080162

2L  FlyBase ncRNA   11011162    11012135    .   -   .   ID=transcript:FBtr0346761;Parent=gene:FBgn0267425;Name=asRNA:CR45778-RA;biotype=ncRNA;transcript_id=FBtr0346761

2L  FlyBase mRNA    11011312    11012135    .   +   .   ID=transcript:FBtr0080156;Parent=gene:FBgn0250837;Name=dUTPase-RB;biotype=protein_coding;transcript_id=FBtr0080156

2L  FlyBase mRNA    11011312    11012135    .   +   .   ID=transcript:FBtr0331195;Parent=gene:FBgn0250837;Name=dUTPase-RC;biotype=protein_coding;transcript_id=FBtr0331195

2L  FlyBase mRNA    11011312    11012135    .   +   .   ID=transcript:FBtr0080157;Parent=gene:FBgn0250837;Name=dUTPase-RA;biotype=protein_coding;transcript_id=FBtr0080157

2L  FlyBase mRNA    67043   71081   .   +   .   ID=transcript:FBtr0306536;Parent=gene:FBgn0067779;Name=dbr-RC;biotype=protein_coding;transcript_id=FBtr0306536

2L  FlyBase mRNA    67043   71390   .   +   .   ID=transcript:FBtr0078100;Parent=gene:FBgn0067779;Name=dbr-RB;biotype=protein_coding;transcript_id=FBtr0078100

file2.txt

FBtr0306536

FBtr0078100

FBtr0306536

FBtr0078100

我的代码: grep 'ID=transcript:' file1.txt | grep -w -f file2.txt

2L  FlyBase mRNA    67043   71081   .   +   .   ID=transcript:FBtr0306536;Parent=gene:FBgn0067779;Name=dbr-RC;biotype=protein_coding;transcript_id=FBtr0306536

2L  FlyBase mRNA    67043   71390   .   +   .   ID=transcript:FBtr0078100;Parent=gene:FBgn0067779;Name=dbr-RB;biotype=protein_coding;transcript_id=FBtr0078100

--->但是,我希望我能得到这样的结果:

2L  FlyBase mRNA    67043   71081   .   +   .   ID=transcript:FBtr0306536;Parent=gene:FBgn0067779;Name=dbr-RC;biotype=protein_coding;transcript_id=FBtr0306536

2L  FlyBase mRNA    67043   71390   .   +   .   ID=transcript:FBtr0078100;Parent=gene:FBgn0067779;Name=dbr-RB;biotype=protein_coding;transcript_id=FBtr0078100

2L  FlyBase mRNA    67043   71081   .   +   .   ID=transcript:FBtr0306536;Parent=gene:FBgn0067779;Name=dbr-RC;biotype=protein_coding;transcript_id=FBtr0306536

2L  FlyBase mRNA    67043   71390   .   +   .   ID=transcript:FBtr0078100;Parent=gene:FBgn0067779;Name=dbr-RB;biotype=protein_coding;transcript_id=FBtr0078100

【问题讨论】:

    标签: bash awk grep


    【解决方案1】:

    假设这是一个bash shell,如果可以选择其他工具,例如awk,它可能会提供更简单的解决方案

    $ awk -F"[:;]" 'NR==FNR{array[$2]=$0; next} {print array[$0]}' file1 file2
    2L FlyBase mRNA 67043 71081 . + . ID=transcript:FBtr0306536;Parent=gene:FBgn0067779;Name=dbr-RC;biotype=protein_coding;transcript_id=FBtr0306536
    
    2L FlyBase mRNA 67043 71390 . + . ID=transcript:FBtr0078100;Parent=gene:FBgn0067779;Name=dbr-RB;biotype=protein_coding;transcript_id=FBtr0078100
    
    2L FlyBase mRNA 67043 71081 . + . ID=transcript:FBtr0306536;Parent=gene:FBgn0067779;Name=dbr-RC;biotype=protein_coding;transcript_id=FBtr0306536
    
    2L FlyBase mRNA 67043 71390 . + . ID=transcript:FBtr0078100;Parent=gene:FBgn0067779;Name=dbr-RB;biotype=protein_coding;transcript_id=FBtr0078100
    

    通过使用两个分隔符: and ;,您可以将基因 ID 分离到第 2 列,创建一个数组并在第二个文件中匹配它们。

    【讨论】:

    • 有没有办法只获得基因名称?在“名称=”之后?并返回类似: dbr\n dbr\n dbr\n dbr 假设会有许多不同的基因名称,长度不同
    • @AmandaF 你的意思是像$ awk -F"[:;]" 'NR==FNR{gsub("Name=",""); array[$2]=$5; next} { print array[$0]}' file1 file2 吗?
    【解决方案2】:

    用循环来做怎么样?

    while read line
    do
     grep "$line" file1.txt
    done < file2.txt
    

    假设您在 file2.txt 中的 FBtr ID 之间没有空行应该可以工作(在您的问题中,有空行)

    【讨论】:

      猜你喜欢
      • 2015-03-21
      • 2019-09-05
      • 2016-12-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-11-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多