>_ IzzyLab

Hands-on task

This task is step 14 of the course “sed: the stream editor”

sed: plain-text version of an email

medium~5 min

The mailing needs a plain-text version of the HTML email /root/mail.html. Remove all tags — <…>, including tags with attributes like <a href="…"> — keeping the text between them as is. Write the result to /root/mail.txt.

What you'll practise:
  • sed -E
  • negated class
  • quantifier greediness
  • g flag

Topic breakdown

.* is greedy: it grabs as much as possible, so <.*> stretches from the first < to the last > in the line and takes the text between tags with it. sed regexes have no "lazy" quantifiers, and the standard trick is to describe the tag contents with the negated class [^>]*: such a pattern physically can't step over > and ends at the nearest closing bracket. Attributes, quotes and / inside the tag don't get in its way.

What's checked

  1. tags removed
  2. email text preserved

What it looks like