sed多行替换 [英] sed multiline replace

查看:112
本文介绍了sed多行替换的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的示例文本文件:

this is my sample text file :


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

我希望将o/p设置为:

for which i want o/p as :


asdas

this should be there

基本上,我试图在"//<< >>"之间找到行(也包括这些行)并将其删除.

Basically i m trying to find lines between "//<<>>" (including these lines too) and delete them.

我尝试使用sed

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

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

但是有些原因并没有产生正确的输出.包含>"符号的第二个标签在正则表达式中失败.不知道我要去哪里错了吗?

But some how it did not produced correct output. The second tag which contained ">" symbol failed in regex. Not sure where i m going wrong?

任何想法怎么做?

推荐答案

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

If you are trying to delete lines with the literal text 'TAG', try:


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

在您的评论中,TAG可能不是文字的,在这种情况下:

From your comments, it appears that TAG may not be literal, in which case:


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

这可以通过使用其他定界符来简化:

This can be simplified by using a different delimiter:

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

这篇关于sed多行替换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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