我试图在两种模式之间过滤掉文本,我已经看过十几个例子,但没有设法得到我想要的东西:
样本输入:
START LEAVEMEBE text
data
START DELETEME text
data
more data
even more
START LEAVEMEBE text
data
more data
START DELETEME text
data
more
SOMETHING that doesn't start with START
@ sometimes it starts with characters that needs to be escaped...
我想留下来:
START LEAVEMEBE text
data
START LEAVEMEBE text
data
more data
SOMETHING that doesn't start with START
@ sometimes it starts with characters that needs to be escaped...
我试过运行sed:
sed 's/^START DELETEME/,/^[^ ]/d'
并获得包容性删除,我尝试添加“排除”(不确定我是否真的理解这种语法):
sed 's/^START DELETEME/,/^[^ ]/{/^[^ ]/!d}'
但是我的“START DELETEME”系列仍然存在(是的,我可以把它弄出来,但那很难看:)除此之外 – 它还删除了这个样本中的空行,如果它们是空的我想留空行我的结束模式完好)
我想知道是否有办法用一个sed命令来做到这一点.
我有一个awk脚本,做得很好:
BEGIN { flag = 0 }
{
if ($0 ~ "^START DELETEME")
flag=1
else if ($0 !~ "^ ")
flag=0
if (flag != 1)
print $0
}
但正如你所知道的那样“A是为了像蜗牛一样运行的awk”.这需要永远.
提前致谢.
戴夫.
最佳答案 在sed中使用循环:
sed -n '/^START DELETEME/{:l n; /^[ ]/bl};p' input