tags:

views:

1648

answers:

2

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 ?

+2  A: 

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'
William Pursell
Google searches for "sed one-liners" often are very helpful - http://sed.sourceforge.net/sed1line.txt should be one of the first results, and contains this one among many others.
Jefromi
+1  A: 

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>>>$@ )'
William Pursell