Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>I can't see why you'd want to use grep for this, while it can be solved with a trivial XPath expression:</p> <pre><code>//title/text() </code></pre> <p>There are many command line tools for XPath and they're usually bundled with the OS.</p> <p>Answers to <a href="https://stackoverflow.com/q/15461737/1407656">this question on Stack Overflow</a> list a number of such tools.</p> <p>The problem with <code>grep</code> here is that it's a generic tool for text processing and it's not aware of any XML structure. For a very simple scenario, you can <a href="https://stackoverflow.com/a/10783395/1407656">get it working</a>. If the document is complex or if you're using this in a script that will survive months or years and not just a one-off job, you may end up feeling sorry for the results.</p> <p>XPath makes it easy to tell the difference between similarly named tags that appear in different contexts in a document.</p> <pre><code>&lt;article&gt; &lt;author&gt; &lt;name&gt;Jon Doe&lt;/name&gt; &lt;title&gt;Chief Editor&lt;/title&gt; &lt;/author&gt; &lt;title&gt;On the Benefits of grep&lt;/title&gt; &lt;publicationDate&gt;2018-02-12&lt;/publicationDate&gt; &lt;text&gt;blah blah blah&lt;/text&gt; &lt;/article&gt; </code></pre> <p>Extracting the title of the article represented by this document with <code>grep</code> would fail if you used any of the other answers posted here. You could technically write the regular expression to get what you need but it's a lot easier with XPath.</p> <pre><code>/article/title/text() </code></pre> <p>If you know you're dealing with a trivial document and the format doesn't change or if it's a one time job where you can quickly validate the results, you can go for <code>grep</code> as explained by others.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload