簡體   English   中英

刪除特定字符之間的空格以及相鄰的出現

[英]remove spaces between specific characters, with adjacent occurrences

我正在嘗試刪除兩個感嘆號“!”之間的空格。

例如,我嘗試過:

echo 'this is ! a repeated ! ! ! ! character ! ! ! here ! !' | sed 's/! !/!!/g'

所需的輸出將是:

this is ! a repeated !!!! character !!! here !!

但是我得到了這個:

this is ! a repeated !! !! character !! ! here !!

一種方法是重復sed命令,但實際上,我可以使用任意數量的“!” 在我的輸入中,我想不出一種干凈的方法來做到這一點。

使用Perl的環顧斷言:

 perl -pe 's/!\s+(?=!)/!/g' 

(?=!)意思是“后跟一個感嘆號,但不要在匹配的字符串中包含感嘆號,並之前開始下一個匹配”。

使用sed您可以執行循環:

echo 'this is ! a repeated ! ! ! ! character ! ! ! here ! !' |
sed -e :a -e 's/! !/!!/g;ta'

this is ! a repeated !!!! character !!! here !!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM