awk 剪切字符串

awk 剪切字符串

当我运行以下命令时,它会打印整个字符串,例如Note="Peptidase S59%2C nucleoporin"

awk '$3=="mRNA"'  Nitab-v4.5_gene_models_Chr_Edwards2017.gff | head 
Nt01    maker   mRNA    143295  155540  .   +   .   ID=Nitab4.5_0006317g0010.1;Parent=Nitab4.5_0006317g0010;Name=Nitab4.5_0006317g0010.1;_AED=0.08;_eAED=0.08;_QI=0|0.45|0.25|1|0.90|0.75|12|0|1011;Note="Peptidase S59%2C nucleoporin"
Nt01    maker   mRNA    170633  173860  .   +   .   ID=Nitab4.5_0006317g0020.1;Parent=Nitab4.5_0006317g0020;Name=Nitab4.5_0006317g0020.1;_AED=0.26;_eAED=0.26;_QI=15|0|0|0.83|0.6|0.33|6|0|424;Note="Putative S-adenosyl-L-methionine-dependent methyltransferase"
Nt01    maker   mRNA    156516  160996  .   -   .   ID=Nitab4.5_0006317g0030.1;Parent=Nitab4.5_0006317g0030;Name=Nitab4.5_0006317g0030.1;_AED=0.01;_eAED=0.01;_QI=161|1|1|1|0|0.5|2|358|141;Note="Unknown"
Nt01    maker   mRNA    78554   80638   .   -   .   ID=Nitab4.5_0006317g0040.1;Parent=Nitab4.5_0006317g0040;Name=Nitab4.5_0006317g0040.1;_AED=0.02;_eAED=0.02;_QI=0|0|0|1|1|1|3|0|187;Note="Heavy metal-associated domain%2C HMA"
Nt01    maker   mRNA    111288  129916  .   -   .   ID=Nitab4.5_0006317g0050.1;Parent=Nitab4.5_0006317g0050;Name=Nitab4.5_0006317g0050.1;_AED=0.24;_eAED=0.24;_QI=0|0|0|0.5|1|1|2|0|72;Note="Unknown"
Nt01    maker   mRNA    470560  474346  .   +   .   ID=Nitab4.5_0002367g0010.1;Parent=Nitab4.5_0002367g0010;Name=Nitab4.5_0002367g0010.1;_AED=0.11;_eAED=0.11;_QI=0|0|0|1|1|1|14|0|668;Note="Auxin response factor%2C B3 DNA binding domain%2C DNA-binding pseudobarrel domain%2C AUX/IAA protein%2C Aux/IAA-ARF-dimerisation"
Nt01    maker   mRNA    499946  502182  .   +   .   ID=Nitab4.5_0002367g0020.1;Parent=Nitab4.5_0002367g0020;Name=Nitab4.5_0002367g0020.1;_AED=0.26;_eAED=0.26;_QI=0|0.5|0|0.66|0|0|3|0|258;Note="Cellulose synthase"
Nt01    maker   mRNA    496891  497596  .   +   .   ID=Nitab4.5_0002367g0030.1;Parent=Nitab4.5_0002367g0030;Name=Nitab4.5_0002367g0030.1;_AED=0.33;_eAED=0.33;_QI=0|0|0|0.5|0|0.5|2|0|213;Note="Cellulose synthase"
Nt01    maker   mRNA    505125  506853  .   -   .   ID=Nitab4.5_0002367g0040.1;Parent=Nitab4.5_0002367g0040;Name=Nitab4.5_0002367g0040.1;_AED=0.09;_eAED=0.09;_QI=0|0|0|1|0.5|0.66|3|0|230;Note="Zinc finger%2C RING-type%2C Zinc finger%2C RING/FYVE/PHD-type"
Nt01    maker   mRNA    564383  570328  .   +   .   ID=Nitab4.5_0002367g0050.1;Parent=Nitab4.5_0002367g0050;Name=Nitab4.5_0002367g0050.1;_AED=0.08;_eAED=0.08;_QI=75|1|1|1|1|1|6|146|267;Note="SAC3/GANP/Nin1/mts3/eIF-3 p25%2C 26S proteasome non-ATPase regulatory subunit Rpn12"

但是,当我使用以下命令时,字符串会缩短为例如注=“肽酶

awk '$3=="mRNA"'  Nitab-v4.5_gene_models_Chr_Edwards2017.gff | awk '{print $9}' | head 
ID=Nitab4.5_0006317g0010.1;Parent=Nitab4.5_0006317g0010;Name=Nitab4.5_0006317g0010.1;_AED=0.08;_eAED=0.08;_QI=0|0.45|0.25|1|0.90|0.75|12|0|1011;Note="Peptidase
ID=Nitab4.5_0006317g0020.1;Parent=Nitab4.5_0006317g0020;Name=Nitab4.5_0006317g0020.1;_AED=0.26;_eAED=0.26;_QI=15|0|0|0.83|0.6|0.33|6|0|424;Note="Putative
ID=Nitab4.5_0006317g0030.1;Parent=Nitab4.5_0006317g0030;Name=Nitab4.5_0006317g0030.1;_AED=0.01;_eAED=0.01;_QI=161|1|1|1|0|0.5|2|358|141;Note="Unknown"
ID=Nitab4.5_0006317g0040.1;Parent=Nitab4.5_0006317g0040;Name=Nitab4.5_0006317g0040.1;_AED=0.02;_eAED=0.02;_QI=0|0|0|1|1|1|3|0|187;Note="Heavy
ID=Nitab4.5_0006317g0050.1;Parent=Nitab4.5_0006317g0050;Name=Nitab4.5_0006317g0050.1;_AED=0.24;_eAED=0.24;_QI=0|0|0|0.5|1|1|2|0|72;Note="Unknown"
ID=Nitab4.5_0002367g0010.1;Parent=Nitab4.5_0002367g0010;Name=Nitab4.5_0002367g0010.1;_AED=0.11;_eAED=0.11;_QI=0|0|0|1|1|1|14|0|668;Note="Auxin
ID=Nitab4.5_0002367g0020.1;Parent=Nitab4.5_0002367g0020;Name=Nitab4.5_0002367g0020.1;_AED=0.26;_eAED=0.26;_QI=0|0.5|0|0.66|0|0|3|0|258;Note="Cellulose
ID=Nitab4.5_0002367g0030.1;Parent=Nitab4.5_0002367g0030;Name=Nitab4.5_0002367g0030.1;_AED=0.33;_eAED=0.33;_QI=0|0|0|0.5|0|0.5|2|0|213;Note="Cellulose
ID=Nitab4.5_0002367g0040.1;Parent=Nitab4.5_0002367g0040;Name=Nitab4.5_0002367g0040.1;_AED=0.09;_eAED=0.09;_QI=0|0|0|1|0.5|0.66|3|0|230;Note="Zinc
ID=Nitab4.5_0002367g0050.1;Parent=Nitab4.5_0002367g0050;Name=Nitab4.5_0002367g0050.1;_AED=0.08;_eAED=0.08;_QI=75|1|1|1|1|1|6|146|267;Note="SAC3/GANP/Nin1/mts3/eIF-3

作为最终结果,我想检索Nitab4.5_0006317g0010.1,Peptidase S59%2C nucleoporin.

我错过了什么?

先感谢您

答案1

GFF 是制表符分隔格式,但您没有使用制表符。除非您使用-F'\t'BEGIN{FS="\t"},否则 awk 将使用任何空格作为字段分隔符,其中包括空格。由于您正在剪切空格,因此$9在第一个空格处结束。您也不需要两个命令,您要做的是:

$ awk -F'\t' '$3=="mRNA"{print $9}' file.gff 
ID=Nitab4.5_0006317g0010.1;Parent=Nitab4.5_0006317g0010;Name=Nitab4.5_0006317g0010.1;_AED=0.08;_eAED=0.08;_QI=0|0.45|0.25|1|0.90|0.75|12|0|1011;Note="Peptidase S59%2C nucleoporin"
ID=Nitab4.5_0006317g0020.1;Parent=Nitab4.5_0006317g0020;Name=Nitab4.5_0006317g0020.1;_AED=0.26;_eAED=0.26;_QI=15|0|0|0.83|0.6|0.33|6|0|424;Note="Putative S-adenosyl-L-methionine-dependent methyltransferase"
ID=Nitab4.5_0006317g0030.1;Parent=Nitab4.5_0006317g0030;Name=Nitab4.5_0006317g0030.1;_AED=0.01;_eAED=0.01;_QI=161|1|1|1|0|0.5|2|358|141;Note="Unknown"
ID=Nitab4.5_0006317g0040.1;Parent=Nitab4.5_0006317g0040;Name=Nitab4.5_0006317g0040.1;_AED=0.02;_eAED=0.02;_QI=0|0|0|1|1|1|3|0|187;Note="Heavy metal-associated domain%2C HMA"
ID=Nitab4.5_0006317g0050.1;Parent=Nitab4.5_0006317g0050;Name=Nitab4.5_0006317g0050.1;_AED=0.24;_eAED=0.24;_QI=0|0|0|0.5|1|1|2|0|72;Note="Unknown"
ID=Nitab4.5_0002367g0010.1;Parent=Nitab4.5_0002367g0010;Name=Nitab4.5_0002367g0010.1;_AED=0.11;_eAED=0.11;_QI=0|0|0|1|1|1|14|0|668;Note="Auxin response factor%2C B3 DNA binding domain%2C DNA-binding pseudobarrel domain%2C AUX/IAA protein%2C Aux/IAA-ARF-dimerisation"
ID=Nitab4.5_0002367g0020.1;Parent=Nitab4.5_0002367g0020;Name=Nitab4.5_0002367g0020.1;_AED=0.26;_eAED=0.26;_QI=0|0.5|0|0.66|0|0|3|0|258;Note="Cellulose synthase"
ID=Nitab4.5_0002367g0030.1;Parent=Nitab4.5_0002367g0030;Name=Nitab4.5_0002367g0030.1;_AED=0.33;_eAED=0.33;_QI=0|0|0|0.5|0|0.5|2|0|213;Note="Cellulose synthase"
ID=Nitab4.5_0002367g0040.1;Parent=Nitab4.5_0002367g0040;Name=Nitab4.5_0002367g0040.1;_AED=0.09;_eAED=0.09;_QI=0|0|0|1|0.5|0.66|3|0|230;Note="Zinc finger%2C RING-type%2C Zinc finger%2C RING/FYVE/PHD-type"
ID=Nitab4.5_0002367g0050.1;Parent=Nitab4.5_0002367g0050;Name=Nitab4.5_0002367g0050.1;_AED=0.08;_eAED=0.08;_QI=75|1|1|1|1|1|6|146|267;Note="SAC3/GANP/Nin1/mts3/eIF-3 p25%2C 26S proteasome non-ATPase regulatory subunit Rpn12"

如果你只想获取 的值Note=,你可以这样做:

$ awk -F"\t" '$3=="mRNA"{sub(/.*Note=/,"",$9); print $9}' file.gff 
"Peptidase S59%2C nucleoporin"
"Putative S-adenosyl-L-methionine-dependent methyltransferase"
"Unknown"
"Heavy metal-associated domain%2C HMA"
"Unknown"
"Auxin response factor%2C B3 DNA binding domain%2C DNA-binding pseudobarrel domain%2C AUX/IAA protein%2C Aux/IAA-ARF-dimerisation"
"Cellulose synthase"
"Cellulose synthase"
"Zinc finger%2C RING-type%2C Zinc finger%2C RING/FYVE/PHD-type"
"SAC3/GANP/Nin1/mts3/eIF-3 p25%2C 26S proteasome non-ATPase regulatory subunit Rpn12"

并且,要删除注释开头和结尾的引号,同时保留可能属于注释本身的部分:

$ awk -F"\t" '$3=="mRNA"{sub(/.*Note=/,"",$9); print $9}' file.gff | sed 's/^"//; s/"$//'
Peptidase S59%2C nucleoporin
Putative S-adenosyl-L-methionine-dependent methyltransferase
Unknown
Heavy metal-associated domain%2C HMA
Unknown
Auxin response factor%2C B3 DNA binding domain%2C DNA-binding pseudobarrel domain%2C AUX/IAA protein%2C Aux/IAA-ARF-dimerisation
Cellulose synthase
Cellulose synthase
Zinc finger%2C RING-type%2C Zinc finger%2C RING/FYVE/PHD-type
SAC3/GANP/Nin1/mts3/eIF-3 p25%2C 26S proteasome non-ATPase regulatory subunit Rpn12

Note最后,要获取和的值ID,您可以这样做:

$ awk -F"\t" '$3=="mRNA"{n=$9; sub(/.*Note="/,"",n); sub(/"$/,"",n); sub(/.*ID=/,"",$9); sub(/;.*/,"",$9); print $9","n}' file.gff 
Nitab4.5_0006317g0010.1,Peptidase S59%2C nucleoporin
Nitab4.5_0006317g0020.1,Putative S-adenosyl-L-methionine-dependent methyltransferase
Nitab4.5_0006317g0030.1,Unknown
Nitab4.5_0006317g0040.1,Heavy metal-associated domain%2C HMA
Nitab4.5_0006317g0050.1,Unknown
Nitab4.5_0002367g0010.1,Auxin response factor%2C B3 DNA binding domain%2C DNA-binding pseudobarrel domain%2C AUX/IAA protein%2C Aux/IAA-ARF-dimerisation
Nitab4.5_0002367g0020.1,Cellulose synthase
Nitab4.5_0002367g0030.1,Cellulose synthase
Nitab4.5_0002367g0040.1,Zinc finger%2C RING-type%2C Zinc finger%2C RING/FYVE/PHD-type
Nitab4.5_0002367g0050.1,SAC3/GANP/Nin1/mts3/eIF-3 p25%2C 26S proteasome non-ATPase regulatory subunit Rpn12

然而,就我个人而言,我会在 perl 中执行此操作:

$ perl -F'\t' -lane 'if($F[2] eq "mRNA"){/ID=([^\;]+).*Note="([^"]+)/; print "$1,$2"}' file.gff 
Nitab4.5_0006317g0010.1,Peptidase S59%2C nucleoporin
Nitab4.5_0006317g0020.1,Putative S-adenosyl-L-methionine-dependent methyltransferase
Nitab4.5_0006317g0030.1,Unknown
Nitab4.5_0006317g0040.1,Heavy metal-associated domain%2C HMA
Nitab4.5_0006317g0050.1,Unknown
Nitab4.5_0002367g0010.1,Auxin response factor%2C B3 DNA binding domain%2C DNA-binding pseudobarrel domain%2C AUX/IAA protein%2C Aux/IAA-ARF-dimerisation
Nitab4.5_0002367g0020.1,Cellulose synthase
Nitab4.5_0002367g0030.1,Cellulose synthase
Nitab4.5_0002367g0040.1,Zinc finger%2C RING-type%2C Zinc finger%2C RING/FYVE/PHD-type
Nitab4.5_0002367g0050.1,SAC3/GANP/Nin1/mts3/eIF-3 p25%2C 26S proteasome 

答案2

假设每行末尾的带引号的字符串不能包含制表符或 a ;,这可能就是您所需要的:

$ awk -F'\t' -v OFS=',' '$3=="mRNA"{ gsub(/;/,FS); gsub(/[^\t=]+=|"/,""); print $9, $15 }' file
Nitab4.5_0006317g0010.1,Peptidase S59%2C nucleoporin
Nitab4.5_0006317g0020.1,Putative S-adenosyl-L-methionine-dependent methyltransferase
Nitab4.5_0006317g0030.1,Unknown
Nitab4.5_0006317g0040.1,Heavy metal-associated domain%2C HMA
Nitab4.5_0006317g0050.1,Unknown
Nitab4.5_0002367g0010.1,Auxin response factor%2C B3 DNA binding domain%2C DNA-binding pseudobarrel domain%2C AUX/IAA protein%2C Aux/IAA-ARF-dimeriion
Nitab4.5_0002367g0020.1,Cellulose synthase
Nitab4.5_0002367g0030.1,Cellulose synthase
Nitab4.5_0002367g0040.1,Zinc finger%2C RING-type%2C Zinc finger%2C RING/FYVE/PHD-type
Nitab4.5_0002367g0050.1,SAC3/GANP/Nin1/mts3/eIF-3 p25%2C 26S proteasome non-ATPase regulatory subunit Rpn12

或(原始想法):

$ cat tst.awk
BEGIN { FS="\t"; OFS="," }
$3 == "mRNA" {
    split($NF,f,/;/)
    for (i in f) {
        gsub(/^[^=]+="?|"$/,"",f[i])
    }
    print f[1], f[7]
}

$ awk -f tst.awk file
Nitab4.5_0006317g0010.1,Peptidase S59%2C nucleoporin
Nitab4.5_0006317g0020.1,Putative S-adenosyl-L-methionine-dependent methyltransferase
Nitab4.5_0006317g0030.1,Unknown
Nitab4.5_0006317g0040.1,Heavy metal-associated domain%2C HMA
Nitab4.5_0006317g0050.1,Unknown
Nitab4.5_0002367g0010.1,Auxin response factor%2C B3 DNA binding domain%2C DNA-binding pseudobarrel domain%2C AUX/IAA protein%2C Aux/IAA-ARF-dimerisation
Nitab4.5_0002367g0020.1,Cellulose synthase
Nitab4.5_0002367g0030.1,Cellulose synthase
Nitab4.5_0002367g0040.1,Zinc finger%2C RING-type%2C Zinc finger%2C RING/FYVE/PHD-type
Nitab4.5_0002367g0050.1,SAC3/GANP/Nin1/mts3/eIF-3 p25%2C 26S proteasome non-ATPase regulatory subunit Rpn12

如果它可以包含 a,那么您应该重新考虑用作,OFS 并使用 tab 或;代替。

答案3

假设您的“最终结果”输出格式是从ID=andNote=字段派生的,则以下内容应该有效:

$ awk -F'\t' '$3=="mRNA"{split($9,f,";"); split(f[1],i,"="); split(f[7],n,"\""); printf("%s,%s\n",i[2],n[2])}' file.gff
Nitab4.5_0006317g0010.1,Peptidase S59%2C nucleoporin
Nitab4.5_0006317g0020.1,Putative S-adenosyl-L-methionine-dependent methyltransferase
Nitab4.5_0006317g0030.1,Unknown
Nitab4.5_0006317g0040.1,Heavy metal-associated domain%2C HMA
Nitab4.5_0006317g0050.1,Unknown
Nitab4.5_0002367g0010.1,Auxin response factor%2C B3 DNA binding domain%2C DNA-binding pseudobarrel domain%2C AUX/IAA protein%2C Aux/IAA-ARF-dimerisation
Nitab4.5_0002367g0020.1,Cellulose synthase
Nitab4.5_0002367g0030.1,Cellulose synthase
Nitab4.5_0002367g0040.1,Zinc finger%2C RING-type%2C Zinc finger%2C RING/FYVE/PHD-type
Nitab4.5_0002367g0050.1,SAC3/GANP/Nin1/mts3/eIF-3 p25%2C 26S proteasome non-ATPase regulatory subunit Rpn12

这会将第 9 个制表符分隔字段拆分为;存储在数组中的分隔子字段f,其中第一个子字段 ( ID=...) 在 处拆分=,第 7 个子字段 ( Note=" ... ") 在 处"拆分,以将感兴趣的部分分隔到辅助数组中in,分别。然后打印这些的相关部分。

这允许相当紧凑的代码,但不一定是最有效的方法。

答案4

使用 GNU awk:

awk -F'\t' -vOFS=, 'NF>=9&&$3=="mRNA"{
N = split($9, a, /[=;]/)
for ( i=1; i<=N; i+=2 )
    h[a[i]] = a[i+1]
print h["ID"], gensub(/"(.*)"/, "\\1", "g", h["Note"])
}' bioinformatic.file

Nitab4.5_0006317g0010.1,Peptidase S59%2C nucleoporin

相关内容