How to match a string not followed by a word using sed

后端 未结 4 594
-上瘾入骨i
-上瘾入骨i 2021-01-24 10:11

I need to delete all strings consisting of a hyphen followed by a whitespace, but only when the whitespace is not followed by the word \"og\". Example file:

Kult         


        
4条回答
  •  情书的邮戳
    2021-01-24 10:55

    You can also use a sed chain, first replacing - og with something nonsensical (like booogabooga), then performing the replacement, then reversing the booogabooga.

    sed -e 's/- og/booogabooga/g; s/- //g; s/booogabooga/- og/g'
    

    Some versions of sed may need:

    sed -e 's/- og/booogabooga/g' -e 's/- //g' -e 's/booogabooga/- og/g'
    

    This can be slower and more painful, especially if you have multiple replacements as @Kusalananda suggests, but it is easier to understand.

提交回复
热议问题