sed multiline替换

rba*_*kar 15 sed multiline

这是我的示例文本文件:

asdas
//<<<TAG
this should be removed
//TAG>>>
this should be there
//<<<TAG
T
>
asd
asd
//TAG>>>

我希望o/p为:

asdas

this should be there

基本上我试图找到"// << >>"之间的行(包括这些行)并删除它们.

我尝试使用sed

sed -n'1h; 1!H; $ {; g; s /// <<]*TAG >>> // g; p;}'<test.txt

但有些人如何没有产生正确的输出.正则表达式中包含">"符号的第二个标记失败.不知道我哪里出错了?

知道怎么做吗?

Wil*_*ell 11

如果您尝试删除文字文本为"TAG"的行,请尝试:

sed '/\/\/<<<TAG/,/\/\/TAG>>>/d'

根据您的评论,似乎TAG可能不是文字,在这种情况下:

sed '/^\/\/<</,/^\/\/.*>>/d'

这可以通过使用不同的分隔符来简化:

sed '@^//<<<@,@^//.*>>>@d'
Run Code Online (Sandbox Code Playgroud)

  • 谷歌搜索"sed one-liners"通常非常有帮助 - http://sed.sourceforge.net/sed1line.txt应该是最早的结果之一,并且包含这一个以及其他许多结果. (4认同)