4

I found a really great tutorial explaining some practical sed examples.

The last one (number 10) can be seen below:

$ sed -e 's/<[^>]*>//g'
This <b> is </b> an <i>example</i>.
This  is  an example.

Can someone please help me through this one?

To summarize where I'm at: 1. I understand : s/x/y/g is a command telling sed to "globally subsitute the regex x with the regex y 2. It seems like the -e flag puts sed in some sort of "interactive mode", from the man page:

-e command
             Append the editing commands specified by the command argument to
             the list of commands.

This seems confusing to me because it doesn't seem like we're giving sed a "list of commands" it seems like we're giving it a "list of arguments" so I'm not to sure on that one. 3. I understand that the first and only < is nothing more than the single char regex <, and the last > is nothing more than the single char regex > 4. I understand that the * is telling sed to match 0 or more occurances of the pattern before it, which is in this case inside the brackets; however, this is where I'm really confused: can someone please unpack the [^>]* more for me?

so where I'm really confused on is:

  • what's going on with -e in plain english?
  • what's going on with [^>]*?

Thanks :)

  • 2
    After you have learned the sed syntax, understand that removing HTML using simple sed substitutions isn't going to be perfect, ever: stackoverflow.com/a/1732454/308668. sed could be able to parse it using its other features, though. – souser12345 Sep 1 '16 at 5:27
5

The sequence s/<[^>]*>//g is a command to the sed processing engine; it tells it to do a "Search and replace". So -e 's/..../g' means "add this search and replace command to the execution of sed.

This may make more sense if we do multiple commands in one command:

sed -e '1d' -e '$d'

would add two commands to the sed processing; "delete first line" and "delete last line".

The [^>] is a regular expression that means "any character except for the >

So [^>]* means "zero or more of any character except for the >

And so <[^>]*> means a <, optionally followed by non->, followed by >.

And then we put that into a "search and replace" command where this sequence is replaced by nothing, and then do it multiple times in the line (the final g).

This means that the string hello <abc> there <def> will first match <abc> (the <, then abc matches "zero or more non->", then the >) and replace that with nothing, and then redo this for the <def>. The result would be hello there.

(Note the extra spaces, 'cos we're not removing them!)

2

Limiting the scope to the two points:

  • The -e tells sed that there is an editing command following

  • The [^>]* is a pattern that matches zero-or-more characters that can be "anything" except the > character

and the editing command says

's/<[^>]*>//g'

change anything that matches <, followed by any number of characters up until the first > to nothing. And do that as many times as there are matches.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.