如何使用Sed替换空格通配符

时间:2019-02-17 04:04:09

标签: sed

这是我的输入:

list toblock '123
456'

我将如何使用sed将通配符替换list toblock,但该代码无法使用空格

sed -i "s/list toblock '*.'/list maclist 'sampletoreplace'/g"

1 个答案:

答案 0 :(得分:1)

您需要的是.*,而不是*..表示任何字符,*表示可以重复任何时间,包括0次)。
另外,sed是行流编辑器,因此默认情况下,匹配项将限制在当前行内。

如果要匹配多行,最简单的方法是使用-z开关(GNU sed):

echo "list toblock '123
456'"|sed -z "s/list toblock '.*'/list maclist 'sampletoreplace'/g"
list maclist 'sampletoreplace'

但是,由于sed通常与RegEx贪婪模式一起使用,并且不支持.*?(停止贪婪模式),因此您可能需要更改为:

sed -z "s/list toblock '[^']*'/list maclist 'sampletoreplace'/g"

其中[^']的意思是any character that is not a '