Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
624 views
in Technique[技术] by (71.8m points)

sed multiline replace

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 ?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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'

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...