0

I have this question:

Use a single SED command to transform the following text:

<h1>this is the h1 header</h1>

<h2>this is the h2 header</h2>

<h3>this is the h3 header</h3>

<h4>this is the h4 header</h4>

to this text:

  <div>this is the h1 header</div>

  <div>this is the h2 header</div>

  <div>this is the h3 header</div>

  <div>this is the h4 header</div>

this the answer I came up with but is there a way to do this in a single command? f3 is my filename

sed -e 's/<h.>/<div>/g' -e 's/<\/h.>/<\/div>/g' f3
Anirudh Giran
  • 55
  • 1
  • 6

2 Answers2

1

You can use

sed 's/h[1-4]/div/g' f3
Ajanyan Pradeep
  • 1,097
  • 1
  • 16
  • 26
0
sed 's|<[^>]*>\(.*\)</[^>]*>|<div>\1</div>|' f3
Quasímodo
  • 3,812
  • 14
  • 25
  • This one worked, but why are we using pipe( | ) in the sed command? What purpose does it serve? – Anirudh Giran Apr 13 '20 at 14:45
  • The delimiter we see most is `/`, but barely any character is valid. The advantage here is that we don't have to escape the `/` in `/div`. – Quasímodo Apr 13 '20 at 14:54