Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Use <code>tac</code> to print the file the other way round and then <code>grep -m1</code> to just print one result. The look behind and look ahead checks text in between <code>&lt;tag&gt;</code> and <code>&lt;/tag&gt;</code>.</p> <pre><code>tac a | grep -m1 -oP '(?&lt;=tag&gt;).*(?=&lt;/tag&gt;)' </code></pre> <h3>Test</h3> <p>Given this file</p> <pre><code>$ cat a &lt;tag&gt; and &lt;/tag&gt; aaa &lt;tag&gt; and &lt;b&gt; other things &lt;/tag&gt; adsaad &lt;tag&gt;and last one&lt;/tag&gt; $ tac a | grep -m1 -oP '(?&lt;=tag&gt;).*(?=&lt;/tag&gt;)' and last one </code></pre> <hr> <h1>Update</h1> <blockquote> <p>EDIT: The search parameters and are contained on different lines with about 100 lines of content separating them. The content is what I'm after...</p> </blockquote> <p>Then it is a bit more tricky:</p> <pre><code>tac file | awk '/&lt;\/tag&gt;/ {p=1; split($0, a, "&lt;/tag&gt;"); $0=a[1]}; /&lt;tag&gt;/ {p=0; split($0, a, "&lt;tag&gt;"); $0=a[2]; print; exit}; p' | tac </code></pre> <p>The idea is to reverse the file and use a flag <code>p</code> to check if the <code>&lt;tag&gt;</code> has appeared yet or not. It will start printing when <code>&lt;/tag&gt;</code> appears and finished when <code>&lt;tag&gt;</code> comes (because we are reading the other way round).</p> <ul> <li><code>split($0, a, "&lt;/tag&gt;"); $0=a[1];</code> gets the data before <code>&lt;/tag&gt;</code></li> <li><code>split($0, a, "&lt;tag&gt;" ); $0=a[2];</code> gets the data after <code>&lt;tag&gt;</code></li> </ul> <h3>Test</h3> <p>Given a file <code>a</code> like this:</p> <pre><code>&lt;tag&gt; and &lt;/tag&gt; aaa &lt;tag&gt; and &lt;b&gt; other thing come here and here &lt;/tag&gt; some text&lt;tag&gt;tag is starting here blabla and ends here&lt;/tag&gt; </code></pre> <p>The output will be:</p> <pre><code>$ tac a | awk '/&lt;\/tag&gt;/ {p=1; split($0, a, "&lt;/tag&gt;"); $0=a[1]}; /&lt;tag&gt;/ {p=0; split($0, a, "&lt;tag&gt;"); $0=a[2]; print; exit}; p' | tac tag is starting here blabla and ends here </code></pre>
 

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