替换指定的字符串和所有后续字符

时间:2016-10-23 15:48:53

标签: regex bash sed text-parsing

我想使用Sed替换所有字符之后包括指定的字符串,如下所示:

关键字

#specific-keyword

输入

Some text amongst which lies a #specific-keyword and everything else that follows.

输出

Some text amongst which lies a 

我想出了类似的东西:

"Some text amongst which lies a #specific-keyword and everything else that follows." | sed "s/#specific-keyword*/ /g" 

但它只会删除特定的关键字,而不会删除之后的字词。

1 个答案:

答案 0 :(得分:0)

我刚刚想出答案,我的正则表达式错了。我需要将.*添加到特定关键字的末尾,如下所示:

sed 'Some text amongst which lies a #specific-keyword and everything else that follows./#specific-keyword.*/ /g'