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

后端 未结 4 493
-上瘾入骨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:48

    Given this input file (I added - ellers since you said in a comment you need to handle them too):

    $ cat file
    Kultur- og idrettsavdelinga skapar- eller nyska- pande kunst og utvik- lar- eller samfunnet
    

    here's the common sed idiomatic approach:

    $ sed 's/a/aA/g; s/- og/aB/g; s/- eller/aC/g; s/- //g; s/aC/- eller/g; s/aB/- og/g; s/aA/a/g' file
    Kultur- og idrettsavdelinga skapar- eller nyskapande kunst og utviklar- eller samfunnet
    

    The above works by turning all as (or whatever other char you like that's not in your target strings) into aA so we can then turn the strings we're interested in, - og and - eller, into a, e.g. aB and aC and at that point we know the only occurrences of aB and aC in the input are the newly transformed - og and - eller since all of the existing as are now aA.

    Now we can just remove all remaining -s from the file and then convert the aCs back to - eller and aBs back to - ogs and finally all aAs back to the original as.

提交回复
热议问题