我有一个这样的文件:
输入文件:
Evigen1000005_c0_g1_i1 0.240 1.212 1.408 3.784 2.029 0.963 -1.22409810298695 1 NA NA NA NA PF04597.13;Ribophorin_I;4.6e-148 NA 1;21;0.875 len=569;ExpAA=32.49;First60=12.82;PredHel=1;Topology=o433-450i Q9SFX3 OST1A_ARATH reviewed Dolichyl-diphosphooligosaccharide--protein_glycosyltransferase_subunit_1A_(EC_2.4.99.18)_(Ribophorin_IA)_(RPN-IA)_(Ribophorin-1A) OST1A_RPN1A_At1g76400_F15M4.10 Arabidopsis_thaliana_(Mouse-ear_cress) 614 protein_N-linked_glycosylation_via_asparagine_[GO:0018279] endoplasmic_reticulum_[GO:0005783];_integral_component_of_membrane_[GO:0016021];_membrane_[GO:0016020];_oligosaccharyltransferase_complex_[GO:0008250] dolichyl-diphosphooligosaccharide-protein_glycotransferase_activity_[GO:0004579] 3702.AT1G76400.1; PF04597; IPR007676; 3702 ath:AT1G76400; F15M4.10 2.4.99.18 SUBCELLULAR_LOCATION:_Endoplasmic_reticulum_membrane_{ECO:0000250};_Single-pass_type_I_membrane_protein_{ECO:0000250}. SIGNAL_1_25_{ECO:0000255}. AT1G76400.1; NA NA NA NA NA NA NA NA NA NA NA
Evigen1000006_c0_g1_i1 0.358 0.179 0.000 0.424 0.139 0.183 NA NA NA NA NA NA PF07767.10;Nop53_(60S_ribosomal_biogenesis);5.2e-21 NA 1;31;0.588 len=170;ExpAA=14.33;First60=14.27;PredHel=0;Topology=o O22892 NOP53_ARATH reviewed Ribosome_biogenesis_protein_NOP53 At2g40430_T2P4.22 Arabidopsis_thaliana_(Mouse-ear_cress) 442 ribosomal_large_subunit_assembly_[GO:0000027];_ribosomal_large_subunit_export_from_nucleus_[GO:0000055] nucleolus_[GO:0005730];_nucleoplasm_[GO:0005654] rRNA_binding_[GO:0019843] 3702.AT2G40430.2; PF07767; IPR011687; 3702 ath:AT2G40430; T2P4.22 SUBCELLULAR_LOCATION:_Nucleus,_nucleolus_{ECO:0000250|UniProtKB:Q9NZM5}._Nucleus,_nucleoplasm_{ECO:0000250|UniProtKB:Q9NZM5}. AT2G40430.1_[O22892-1]; NA NA NA NA NA NA NA NA NA NA NA NA NA
我想从括号中取出仅以“GO:”开头的文本。每个 GO 之后:有 7 位数字。例如“GO:0018279”。它们是 GO 术语。每行中 GO 项的数量不相等。输出必须是一个文件,其中第一列包含 Untranscript id(例如 Evigen1000005_c0_g1_i1)和其余 GO 术语。我想要一个像这样的输出文件:
输出文件:
Evigen1000005_c0_g1_i1 GO:0018279 GO:0005783 GO:0016021 GO:0016020 GO:0008250 GO:0004579
Evigen1000006_c0_g1_i1 GO:0000027 GO:0000055 GO:0005730 GO:0005654 GO:0019843
答案1
建议script
,符合您的最终规格。
#!/bin/bash
while read line
do
# echo "$line"
name=${line%% *}
echo -n "$name "
data=$(<<< "$line" grep -o '\[GO:.\{7\}\]' | tr '\n' ' ' | sed -e 's/\[//g' -e 's/\]//g')
echo "$data"
done < "$1"
测试:
$ ./script input
Evigen1000005_c0_g1_i1 GO:0018279 GO:0005783 GO:0016021 GO:0016020 GO:0008250 GO:0004579
Evigen1000006_c0_g1_i1 GO:0000027 GO:0000055 GO:0005730 GO:0005654 GO:0019843
答案2
怎么样
sed -r 's/(^[^[:space:]]* )[^[]*\[/\1/; s/\][^[]*(\[|$)/ /g' file
Evigen1000005_c0_g1_i1 GO:0018279 GO:0005783 GO:0016021 GO:0016020 GO:0008250 GO:0004579
Evigen1000006_c0_g1_i1 GO:0000027 GO:0000055 GO:0005730 GO:0005654 GO:0019843 O22892-1
您所需的输出不反映已处理的输入样本。
编辑:甚至
sed -r 's/((^[^ ]* )|\])[^[]*(\[|$)/\2 /g' file
编辑:将您的问题和所需的输出修改三次,尝试
sed -r 's/((^[^ ]* )|\])[^[]*(\[GO)/\2 GO/g; s/\].*$//' file
答案3
在 sed 中更快:
start='\[GO:' end='\]'
sed -e 's,'"$start"$',\1,g' -e 's,'"$end"$',\2,g' \
-e $'s, [^\1]*, ,' -e $'s,\1\\([^\2]*\\)\2[^\1]*,GO:\\1 ,g' \
infile
或 awk:
awk -vone=$'\1' -vtwo=$'\3' -vstart='\[GO:' -v end='\]' '
{
printf("%s ",$1);
gsub(start,one);
gsub(end,two);
sub("^[^"one"]*"one,"GO:")
gsub(two"[^"one"]*"one," GO:")
sub(two".*$" ,"")
}
1' infile