同一行上的多个grep输出

时间:2017-08-01 08:56:20

标签: bash grep echo cut

这似乎是一个非常微不足道的问题,但我没有足够的经验使用grepecho来自己回答。我看了herehere但没有成功。

我有一个像这样(.gff文件)的文件超过1,000,000行。

NW_007577731.1  RefSeq  region  1   3345205 .   +   .   ID=id0;Dbxref=taxon:144197;Name=Unknown;chromosome=Unknown;collection-date=16-Aug-2005;country=USA: Emerald Reef%2C Florida;gbkey=Src;genome=genomic;isolate=25-593;lat-lon=25.6748 N 80.0982 W;mol_type=genomic DNA;sex=male
NW_007577731.1  Gnomon  gene    7982    24854   .   -   .   ID=gene0;Dbxref=GeneID:103352799;Name=LOC103352799;gbkey=Gene;gene=LOC103352799;gene_biotype=protein_coding
NW_007577731.1  Gnomon  mRNA    7982    24854   .   -   .   ID=rna0;Parent=gene0;Dbxref=GeneID:103352799,Genbank:XM_008279367.1;Name=XM_008279367.1;gbkey=mRNA;gene=LOC103352799;model_evidence=Supporting evidence includes similarity to: 22 Proteins%2C and 73%25 coverage of the annotated genomic feature by RNAseq alignments;product=homer protein homolog 3-like;transcript_id=XM_008279367.1
NW_007577731.1  RefSeq  region  1   3345205 .   +   .   ID=id0;Dbxref=taxon:144197;Name=Unknown;chromosome=Unknown;collection-date=16-Aug-2005;country=USA: Emerald Reef%2C Florida;gbkey=Src;genome=genomic;isolate=25-593;lat-lon=25.6748 N 80.0982 W;mol_type=genomic DNA;sex=male
NW_007577731.1  Gnomon  gene    7982    24854   .   -   .   ID=gene0;Dbxref=GeneID:103352799;Name=LOC103352799;gbkey=Gene;gene=LOC103352799;gene_biotype=protein_coding
NW_007577731.1  Gnomon  mRNA    7982    24854   .   -   .   ID=rna0;Parent=gene0;Dbxref=GeneID:103352799,Genbank:XM_008279367.1;Name=XM_008279367.1;gbkey=mRNA;gene=LOC103352799;model_evidence=Supporting evidence includes similarity to: 22 Proteins%2C and 73%25 coverage of the annotated genomic feature by RNAseq alignments;product=homer protein homolog 3-like;transcript_id=XM_008279367.1

我想在第三列中包含mRNA的行上进行grep,以获得此标签分隔的输出(字段gene=product=transcript_id=中的值)。

LOC103352799    homer protein homolog 3-like    XM_008279367.1
LOC103352799    homer protein homolog 3-like    XM_008279367.1

由于缺乏优雅,我可以使用

分别获得3列
grep "mRNA\t" myfile.gff|sed s/gene=/@/|cut -f2 -d"@" |cut -f1 -d";"
grep "mRNA\t" myfile.gff|sed s/product=/@/|cut -f2 -d"@" |cut -f1 -d";"
grep "mRNA\t" myfile.gff|sed s/transcript_id=/@/|cut -f2 -d"@" |cut -f1 -d";"

但是如何将这3个命令的输出附加在同一行?我试过了

echo -e "`grep "mRNA\t" myfile.gff|sed s/gene=/@/|cut -f2 -d"@" |cut -f1 -d";"`\t`grep "mRNA\t" myfile.gff|sed s/product=/@/|cut -f2 -d"@" |cut -f1 -d";"`\t`grep "mRNA\t" myfile.gff|sed s/transcript_id=/@/|cut -f2 -d"@" |cut -f1 -d";"`"

但这是输出:

LOC103352799
LOC103352799    homer protein homolog 3-like
homer protein homolog 3-like    XM_008279367.1
XM_008279367.1

非常感谢你的帮助!

2 个答案:

答案 0 :(得分:2)

使用awk:

$ awk 'BEGIN {
    FS=OFS="\t"                       # field separators to tab
    k="gene,product,transcript_id"    # keyword list
    split(k,a,",")                    # split keywords to a hash for matching
    for(i in a)                       # values to keys
        p[a[i]]
}
$3=="mRNA" {
    b=""                              # reset buffer b
    split($9,a,"[=;]")                # split the data to a hash
    for(i in a)                       # iterate and search
        if(a[i] in p)                 # ... for keywords, if match, 
            b=b (b==""?"":OFS) a[i+1] # ... value is the next, buffer
    print b                           # output buffer
}' file
LOC103352799    homer protein homolog 3-like    XM_008279367.1
LOC103352799    homer protein homolog 3-like    XM_008279367.1

答案 1 :(得分:0)

谈论oneliners,这是sed中的一个:

sed -nE '/\tmRNA\t/ { s/.*gene=([^;]+).*product=([^;]+).*transcript_id=([^;]+)/\1\t\2\t\3/g;p }' file

唯一的假设是geneproducttranscript_id字段的固定顺序。这可以通过一些替换来修复,但是考虑到正则表达式的可读性。