删除bash中两个模式之间的部分

时间:2019-04-13 17:54:22

标签: sed

假设我有一个与此相关的文件:

(Ano_gla|EOG091B00FI:0.21327484,Tri_cas|EOG091B00FI:0.14561670,((Tri_bro|EOG091B00FI:0.00523450,Tri_jap|EOG091B00FI:0.01261030)1.00
0000:0.26780267,(((((Orm_nit|EOG091B00FI:0.00243200,Orm_pom|EOG091B00FI:0.00914980)1.000000:0.08747204,(((((Meg_dor|EOG091B00FI:0.0
0953580,Meg_sti|EOG091B00FI:0.02205870)1.000000:0.09005934,(Cer_mar|EOG091B00FI:0.00429740,Cer_sol|EOG091B00FI:0.02112877)1.000000:
0.07852307)0.937000:0.01510878,(((Cec_fun|EOG091B00FI:0.04067119,(Tri_sar|EOG091B00FI:0.00462004,(Nas_gir|EOG091B00FI:0.00126111,Na
s_lon|EOG091B00FI:0.00087461)0.877000:0.00251191)0.995000:0.01752929)1.000000:0.04366313,(Tri_bra|EOG091B00FI:0.00461186,Tri_pre|EO
G091B00FI:0.01023626)1.000000:0.44067486)0.000000:0.01008020,(Ana_pse|EOG091B00FI:0.07264534))

我正在寻找一种bash方法,以删除|:之间的部分

并获得:

(Ano_gla:0.21327484,Tri_cas:0.14561670,((Tri_bro:0.00523450,Tri_jap:0.01261030)1.00
0000:0.26780267,(((((Orm_nit:0.00243200,Orm_pom:0.00914980)1.000000:0.08747204,(((((Meg_dor:0.0
0953580,Meg_sti:0.02205870)1.000000:0.09005934,(Cer_mar:0.00429740,Cer_sol:0.02112877)1.000000:
0.07852307)0.937000:0.01510878,(((Cec_fun:0.04067119,(Tri_sar:0.00462004,(Nas_gir:0.00126111,Na
s_lon:0.00087461)0.877000:0.00251191)0.995000:0.01752929)1.000000:0.04366313,(Tri_bra:0.00461186,Tri_pre:0.01023626)1.000000:0.44067486)0.000000:0.01008020,(Ana_pse:0.07264534

我尝试过:

sed -e 's/\(|\).*\(:\)/\1\2/g' myfile 

但是它不起作用。

3 个答案:

答案 0 :(得分:2)

sed ':a;$!{N;ba};s/|[^:]*//g' myfile

解释:

:a           # Label to jump to
$! {         # On every line but the last one
    N        # Append next line to pattern space
    ba       # Jump to label
}
s/|[^:]*//g  # Remove every pipe up to (and excluding) the next colon

这会将整个文件插入模式空间,然后进行一次全局替换。

请注意,这会保留输入文件的结尾)),与您的预期输出不同。


对于GNU sed以外的其他sed,必须将命令拉开一点以使标签分开:

sed -e ':a' -e '$!{N;ba;}' -e 's/|[^:]*//g' myfile

答案 1 :(得分:1)

$ sed 's/|[^|:]*:/:/g' file
(Ano_gla:0.21327484,Tri_cas:0.14561670,((Tri_bro:0.00523450,Tri_jap:0.01261030)1.00
0000:0.26780267,(((((Orm_nit:0.00243200,Orm_pom:0.00914980)1.000000:0.08747204,(((((Meg_dor:0.0
0953580,Meg_sti:0.02205870)1.000000:0.09005934,(Cer_mar:0.00429740,Cer_sol:0.02112877)1.000000:
0.07852307)0.937000:0.01510878,(((Cec_fun:0.04067119,(Tri_sar:0.00462004,(Nas_gir:0.00126111,Na
s_lon:0.00087461)0.877000:0.00251191)0.995000:0.01752929)1.000000:0.04366313,(Tri_bra:0.00461186,Tri_pre|EO
G091B00FI:0.01023626)1.000000:0.44067486)0.000000:0.01008020,(Ana_pse:0.07264534))

或者如果您的输入确实是跨行的,则使用-z的GNU sed:

$ sed -z 's/|[^|:]*:/:/g' file
(Ano_gla:0.21327484,Tri_cas:0.14561670,((Tri_bro:0.00523450,Tri_jap:0.01261030)1.00
0000:0.26780267,(((((Orm_nit:0.00243200,Orm_pom:0.00914980)1.000000:0.08747204,(((((Meg_dor:0.0
0953580,Meg_sti:0.02205870)1.000000:0.09005934,(Cer_mar:0.00429740,Cer_sol:0.02112877)1.000000:
0.07852307)0.937000:0.01510878,(((Cec_fun:0.04067119,(Tri_sar:0.00462004,(Nas_gir:0.00126111,Na
s_lon:0.00087461)0.877000:0.00251191)0.995000:0.01752929)1.000000:0.04366313,(Tri_bra:0.00461186,Tri_pre:0.01023626)1.000000:0.44067486)0.000000:0.01008020,(Ana_pse:0.07264534))

答案 2 :(得分:1)

如果您的“ d”文件中的数据尝试gnu sed,

sed -E 's/\|[^:]+:/:/g' d