0

I need to get value from a tag with specific title.

I have this command.

sed -n 's/title="view quote">\(.*\)<\/a>/\1/p' index.html

This is part of index.html and i need that 'Everything in life is luck'

<a title="view quote" href="https://www.brainyquote.com/quotes/donald_trump_106578" class="oncl_q">
<img id="qimage_106578" src="./Donald Trump Quotes - BrainyQuote_files/donaldtrump1.jpg" class="bqphtgrid" alt="Everything in life is luck. - Donald Trump">
</a>
</div>
<a href="https://www.brainyquote.com/quotes/donald_trump_106578" class="b-qt qt_106578 oncl_q" title="view quote">Everything in life is luck.</a>
<a href="https://www.brainyquote.com/quotes/donald_trump_106578" class="bq-aut qa_106578 oncl_a" title="view author">Donald Trump</a>
</div>
<div class="qbn-box">
<div class="sh-cont">
<a href="https://www.brainyquote.com/share/fb/106578" aria-label="Share this quote on Facebook" class="sh-fb sh-grey" target="_blank" rel="nofollow"><img src="./Donald Trump Quotes - BrainyQuote_files/facebook-f.svg" alt="Share on Facebook" class="bq-fa"></a><a href="https://www.brainyquote.com/share/tw/106578?ti=Donald+Trump+Quotes" aria-label="Share this quote on Twitter" class="sh-tw sh-grey" target="_blank" rel="nofollow"><img src="./Donald Trump Quotes - BrainyQuote_files/twitter.svg" alt="Share on Twitter" class="bq-fa"></a><a href="https://www.brainyquote.com/share/li/106578?ti=Donald+Trump+Quotes+-+BrainyQuote" aria-label="Share this quote on LinkedIn" class="sh-tw sh-grey" target="_blank" rel="nofollow"><img src="./Donald Trump Quotes - BrainyQuote_files/linkedin-in.svg" alt="Share on LinkedIn" class="bq-fa"></a>
</div>
</div>
<div class="qll-dsk-kw-box">
<div class="kw-box">
<a href="https://www.brainyquote.com/topics/life-quotes" class="qkw-btn btn btn-xs oncl_klc" data-idx="0">Life</a>
<a href="https://www.brainyquote.com/topics/luck-quotes" class="qkw-btn btn btn-xs oncl_klc" data-idx="1">Luck</a>
<a href="https://www.brainyquote.com/topics/everything-quotes" class="qkw-btn btn btn-xs oncl_klc" data-idx="2">Everything</a>
</div>
</div>
</div>
<div id="qpos_1_2" class="m-brick grid-item boxy bqQt r-width" style="position: absolute; left: 623px; top: 2px;">
<div class="clearfix">
<div class="qti-listm">
<a title="view quote" href="https://www.brainyquote.com/quotes/donald_trump_119339" class="oncl_q">
<img id="qimage_119339" src="./Donald Trump Quotes - BrainyQuote_files/donaldtrump1(1).jpg" class="bqphtgrid" alt="The first thing the secretary types is the boss. - Donald Trump">
</a>
</div>
<a href="https://www.brainyquote.com/quotes/donald_trump_119339" class="b-qt qt_119339 oncl_q" title="view quote">The first thing the secretary types is the boss.</a>
<a href="https://www.brainyquote.com/quotes/donald_trump_119339" class="bq-aut qa_119339 oncl_a" title="view author">Donald Trump</a>
</div>
<div class="qbn-box">
<div class="sh-cont">
<a href="https://www.brainyquote.com/share/fb/119339" aria-label="Share this quote on Facebook" class="sh-fb sh-grey" target="_blank" rel="nofollow"><img src="./Donald Trump Quotes - BrainyQuote_files/facebook-f.svg" alt="Share on Facebook" class="bq-fa"></a><a href="https://www.brainyquote.com/share/tw/119339?ti=Donald+Trump+Quotes" aria-label="Share this quote on Twitter" class="sh-tw sh-grey" target="_blank" rel="nofollow"><img src="./Donald Trump Quotes - BrainyQuote_files/twitter.svg" alt="Share on Twitter" class="bq-fa"></a><a href="https://www.brainyquote.com/share/li/119339?ti=Donald+Trump+Quotes+-+BrainyQuote" aria-label="Share this quote on LinkedIn" class="sh-tw sh-grey" target="_blank" rel="nofollow"><img src="./Donald Trump Quotes - BrainyQuote_files/linkedin-in.svg" alt="Share on LinkedIn" class="bq-fa"></a>
</div>
</div>

And i need all this vlaues to fill in array in bash. Expected output from here is ['Everything in life is luck','The first thing the secretary types is the boss.']. But i need all quotes which are in index.html so i need selector to get all quotes to array.

3
  • Why not just grep -o Everything in life is luck index.html? – Nasir Riley Apr 17 at 15:48
  • Sorry bad question, i need to get all values like this. There is hundred of quotes. – ToklCz Apr 17 at 15:50
  • What you want still isn't clear. Do you want all of the text that appears in quotes or just certain text? From that sample text, add the expected output to your question including the circumstances. – Nasir Riley Apr 17 at 15:59
2

Even though it's HTML and not proper XML you can actually do this with xmlstarlet.

Let's call your file index.html. Command invocation:

xmlstarlet fo -H index.html 2>/dev/null |
    xmlstarlet sel -t -v '//a[@title="view quote" and string-length(text()) > 1]' -n 2>/dev/null

Output:

Everything in life is luck.
The first thing the secretary types is the boss.

You may not have come across xmlstarlet before. It's an amazing tool that lets you format, edit, and parse XML. Today I discovered that it can also reformat poorly formed HTML. If you don't have it, install it. (If you don't have rights to install it, ask.) It understands XML in a way that sed and awk cannot begin to handle. Reformat the XML? sed and awk may well break, but xmlstarlet sees no significant difference.

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.