2017-12-18 421 views
0

我想在批處理控制檯中使用sed替換一些字符串。我的輸入包括在文件中有這樣的詩句:在sed命令中正則表達式不正確

$ head qiimetax_sorted.txt 

A61579.1.1437 
D_0__Bacteria;D_1__Thermotogae;D_2__Thermotogae;D_3__Thermotogales; 
D_4__Fervidobacteriaceae;D_5__Fervidobacterium;Ambiguous_taxa;D_7__; 
D_8__;D_9__;D_10__;D_11__;D_12__;D_13__;D_14__ 
AAAA02020712.626.2096 
D_0__Bacteria;D_1__Proteobacteria;D_2__Alphaproteobacteria;D_3__Rhizobiales; 
D_4__Bradyrhizobiaceae;D_5__uncultured;D_6__Oryza sativa 
Indica Group (long-grained rice);D_7__;D_8__;D_9__;D_10__;D_11__;D_12__; 
D_13__;D_14__ 

現在我試圖抹去這個sed命令的名字前的「D_number__」字符串,它沒有任何替換:

sed -r 's/D_\d+__//g' qiimetax_sorted.txt > qiimesed.txt 

任何想法是哪個問題? 謝謝!

+0

也見[爲什麼在X,但不以y我的正則表達式的工作?](HTTPS:/ /unix.stackexchange.com/questions/119905/why-does-my-regular-expression-work-in-x-but-not-in-y) – Sundeep

回答

2

你的正則表達式語法是perl like。

所以,如果你想保留它:

perl -pe 's/D_\d+__//g' qiimetax_sorted.txt > qiimesed.tx 

sed -r 's/D_[0-9]+__//g' qiimetax_sorted.txt > qiimesed.tx 
+2

建議:使用'-E'而不是'-r'。 GNU sed支持並據我所知,其他一些sed版本現在支持'-E' .. – Sundeep