如何使用sed匹配后面没有单词的字符串 [英] How to match a string not followed by a word using sed

查看:46
本文介绍了如何使用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 - 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

这是常见的 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>,例如aBaC 到那时我们知道 aBaC 的唯一出现> 在输入中是新转换的 - og- eller 因为所有现有的 a 现在都是 aA>.

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<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 as are now aA.

现在我们可以从文件中删除所有剩余的 - ,然后将 aC 转换回 -elleraBs 回到 -ogs,最后所有 aAs 回到原来的 as.

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.

这篇关于如何使用sed匹配后面没有单词的字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆