Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

sed multiline replace

Tags:

multiline

sed

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 ?

like image 225
rbawaskar Avatar asked Aug 03 '09 08:08

rbawaskar


People also ask

Can sed replace multiple lines?

By using N and D commands, sed can apply regular expressions on multiple lines (that is, multiple lines are stored in the pattern space, and the regular expression works on it): $ cat two-cities-dup2.

How do you replace a line in a file using sed?

Find and replace text within a file using sed command Use Stream EDitor (sed) as follows: sed -i 's/old-text/new-text/g' input.txt. The s is the substitute command of sed for find and replace. It tells sed to find all occurrences of 'old-text' and replace with 'new-text' in a file named input.txt.


1 Answers

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'
like image 184
William Pursell Avatar answered Sep 24 '22 01:09

William Pursell