sed 多行替换 [英] sed multiline replace

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

问题描述

这是我的示例文本文件:

<前>阿斯达斯//<<<标签这应该被删除//标签>>>这应该在那里//<<<标签吨>阿斯达阿斯达//标签>>>

我想要 o/p 为:

<前>阿斯达斯这应该在那里

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

我尝试使用 sed

<块引用>

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

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

知道怎么做吗?

解决方案

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

<前>sed '///<<>>/d'

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

<前>sed '/^//<>/d'

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

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

this is my sample text file :

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

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.

I tried using sed

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?

Any idea how to do it ?

解决方案

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

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

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天全站免登陆