sed multiline replace

2019-01-26 05:07发布

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 ?

标签: sed multiline
3条回答
Deceive 欺骗
2楼-- · 2019-01-26 05:29

Rather than using the sed solution I gave, you might like either of these in perl and awk:

perl -ne 'print if !( m@//<<<TAG@ .. m@//TAG>>>@ )'
awk '/\/\/<<<TAG/,/\/\/TAG>>>/ {next} 1'

Given that I think you really do not want TAG to be a constant, the cleanest solution I know of is the perl variant:

perl -ne 'print if !( m@^//<<<(.*)@ .. m@^//$1>>>$@ )'
查看更多
等我变得足够好
3楼-- · 2019-01-26 05:34

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'
查看更多
Emotional °昔
4楼-- · 2019-01-26 05:52

In addition the search delimiters in sed can be changed by escaping the first delimiter:

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

Awk version the matches the end with the same tag name:

awk -F'//<<<|//|>>>' '$2{p=$2; while(getline && p!=$2); next}1' file
查看更多
登录 后发表回答