awk选择以特定字符串开头的多列并打印到最后一个字段

时间:2018-03-19 16:23:51

标签: awk

我正在寻找一种皮条客这个命令的方法,它检查以某个字符串开头的列(" product =")并打印相应的列(以及许多后续和第二个和第三个,基于";"作为分隔符。

awk 'BEGIN{FS = ";", OFS = "\t"} 
  {for (i=1;i<=NF;i++){if ($i ~/^product=/) 
  {print $2, $3, $i, $(i+1),$(i+2),$(i+3),$(i+4),$(i+5),$(i+6),$(i+7)}}}' file

对于这样的文件:

contig_19838    Prodigal:2.6    CDS 8893    10215   .   -   0   ID=PROKKA_33099;eC_number=3.5.99.8;gene=naaA;inference=ab initio prediction:Prodigal:2.6,similar to AA sequence:UniProtKB:D3WZ85;locus_tag=PROKKA_33099;product=5-nitroanthranilic acid aminohydrolase
contig_19839    Prodigal:2.6    CDS 207 368 .   -   0   ID=PROKKA_33119;inference=ab initio prediction:Prodigal:2.6;locus_tag=PROKKA_33119;product=hypothetical protein
contig_1984 Prodigal:2.6    CDS 101 853 .   -   0   ID=PROKKA_05585;inference=ab initio prediction:Prodigal:2.6,protein motif:CLUSTERS:PRK09421;locus_tag=PROKKA_05585;product=molybdate ABC transporter permease protein
contig_19840    Prodigal:2.6    CDS 50  352 .   +   0   ID=PROKKA_33120;eC_number=3.1.3.48;gene=cpsB;inference=ab initio prediction:Prodigal:2.6,similar to AA sequence:UniProtKB:Q9AHD4;locus_tag=PROKKA_33120;product=Tyrosine-protein phosphatase CpsB

我想添加以&#34; gene =&#34;开头的列。输出可以在不同的列中,但我不知道如何添加AND / OR语句。

我也很难以&#34;产品&#34;开头打印字符串。因为输出用空格分隔并分成许多列。因此,我打印了相当多的后续专栏(当然看起来很奇怪),因为我不知道如何将其与这里的答案结合起来Using awk to print all columns from the nth to the last

所以我希望有一个输出,如

gene=naaA   product=5-nitroanthranilic acid aminohydrolase
    product=hypothetical protein
    product=molybdate ABC transporter permease protein
gene=cpsB   product=Tyrosine-protein phosphatase CpsB

对于有和没有&#34;基因=&#34;领域。有什么想法吗?

1 个答案:

答案 0 :(得分:1)

考虑到您的实际Input_file与显示的示例相同,如果是,那么您可以尝试关注awk并告诉我这是否对您有帮助。

awk '
{
  match($0,/gene=[^;]*/);
  gene_value=substr($0,RSTART,RLENGTH);
  match($0,/product=.*/);
  print gene_value,substr($0,RSTART,RLENGTH)
}
'   Input_file