Issue
Let's say I have a huge file with this:
(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))
And I'm looking for a bash method in order to remove the part between the |
and :
and get:
(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
I tried:
sed -e 's/\(|\).*\(:\)/\1\2/g' myfile
but it does not work.
Solution
sed ':a;$!{N;ba};s/|[^:]*//g' myfile
Explained:
: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
This slurps the complete file into the pattern space and then does one global substitution.
Notice that this leaves the closing ))
of the input file in place, unlike your expected output.
For seds other than GNU sed, the command has to be pulled apart a bit so that the label is separate:
sed -e ':a' -e '$!{N;ba;}' -e 's/|[^:]*//g' myfile
Answered By - Benjamin W.