如何使用sed匹配后面没有单词的字符串 [英] How to match a string not followed by a word using sed
问题描述
我需要删除所有由连字符后跟空格组成的字符串,但前提是空格后没有跟有单词og".示例文件:
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:
Kultur- og idrettsavdelinga skapar nyska- pande kunst og utvik- lar samfunnet
我尝试了负面预测:
sed -e 's/- (?!og)//g'
但它不起作用.我想要的是这样的:
but it doesn't work. What I want is something like this:
Kultur- og idrettsavdelinga skapar nyskapande kunst og utviklar samfunnet.
有什么想法吗?
推荐答案
鉴于此输入文件(我添加了 -eller
,因为您在评论中说您也需要处理它们):>
Given this input file (I added - eller
s 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
这是常见的 sed 惯用方法:
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
上面的方法是将所有 a
(或您喜欢的任何其他不在目标字符串中的字符)转换为 aA
,这样我们就可以将字符串转换为我们重新对 - og
和 - eller
感兴趣,转化为 a<some other character>
,例如aB
和 aC
到那时我们知道 aB
和 aC
的唯一出现> 在输入中是新转换的 - og
和 - eller
因为所有现有的 a
现在都是 aA
>.
The above works by turning all a
s (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<some other character>
, 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 a
s are now aA
.
现在我们可以从文件中删除所有剩余的 -
,然后将 aC
转换回 -eller
和 aB
s 回到 -og
s,最后所有 aA
s 回到原来的 a
s.
Now we can just remove all remaining -
s from the file and then convert the aC
s back to - eller
and aB
s back to - og
s and finally all aA
s back to the original a
s.
这篇关于如何使用sed匹配后面没有单词的字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!