Extract Text between HTML tags with sed or grep

Viewed 5646

I have a Problem. I want to get two parts of this html in values with the sed or grep command. How i can extract both of them?

test.html:

<html>
 <body>
  <div id="foo" class="foo">
   Some Text.
    <p id="author" class="author">
     <br>
     <a href="example.com">bar</a>
    </p>
  </div>
 </body>
</html>

script.sh

#!/bin/bash

author=$(sed 's/.*<p id="author" class="author"><br><a href="*">\(.*\)<\/a><\/p>.*/\1/p' test.html)
quote=$(sed 's/.*<div id="foo" class="foo">\(.*\)<\/div>.*/\1/p' test.html)

Under the line i want only the text in the values. without the html tags. But my script doesent works..

4 Answers

You can use html2text

# cat test.html | html2text
Some Text.


[bar](example.com)

I'm using very often with curl

# curl -s http://www.example.com/ | html2text

# Example Domain

This domain is for use in illustrative examples in documents. You may use this
domain in literature without prior coordination or asking for permission.

[More information...](https://www.iana.org/domains/example)

#

Please don't use regex to parse HTML/XML, but use a dedicated parser like instead:

$ xidel -s test.html -e '//p/a,//div/normalize-space(text())'
bar
Some Text.
$ eval $(xidel test.html -se 'author:=//p/a,quote:=//div/normalize-space(text())' --output-format=bash)

$ printf '%s\n' "$author" "$quote"
bar
Some Text.
Related