Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sed regexp multiline - replace HTML

I am attempting to replace multiple lines using sed on a Linux system

Here is my file

<!-- PAGE TAG -->
DATA1
DATA2
DATA3
DATA4
DATA5
DATA6
<div id="DATA"></div>
DATA8
DATA9
<!-- PAGE TAG -->

The attempts I have made and failed!

sed -n '1h;1!H;${;g;s/<!-- PAGE TAG -->.*<!-- PAGE TAG -->//g;p;}' 
sed -n '1!N; s/<!-- PAGE TAG -->.*<!-- PAGE TAG -->// p'
sed -i 's|<!--[^>]*-->[^+]+<!--[^>]*-->||g' 
sed -i 's|/\/\/<!-- PAGE TA -->/,/\/\/<!-- PAGE TA -->||g'

Everything in between <!-- PAGE TAG --> should be replaced.

This question is similar sed multiline replace

like image 679
Christopher Wilson Avatar asked Feb 20 '23 03:02

Christopher Wilson


1 Answers

While @nhahtdh's answer is the correct one for your original question, this solution is the answer to your comments:

sed '
  /<!-- PAGE TAG -->/,/<!-- PAGE TAG -->/ {
    1 {
      s/^.*$/Replace Data/
      b
    }
    d
  }
'

You can read it like so:

/<!-- PAGE TAG -->/,/<!-- PAGE TAG -->/ -> for the lines between these regexes

1 { -> for the first matching line

s/^.*$/Replace Data/ -> search for anything and replace with Replace Data

b -> branch to end (behaves like break in this instance)

d -> otherwise, delete the line

You can make any series of sed commands into one-liners with gnu sed by adding semicolons after each command (but it's not recommended if you want to be able to read it later on):

sed '/<!-- PAGE TAG -->/,/<!-- PAGE TAG -->/ { 1 { s/^.*$/Replace Data/; b; }; d; };'

Just as a side note, you should really try to be as specific as possible in your posting. "replaced/removed" means "replaced OR removed". If you want it replaced, just say replaced. That helps both those of us trying to answer your question and future users who might be experiencing the same issue.

like image 166
Tim Pote Avatar answered Feb 27 '23 09:02

Tim Pote