Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p><strong>Edit:</strong> It's been noted my first solution was a little off. Here's an attempt at a fix, though I haven't tested it yet due to lack of place to test it.</p> <p>I've come up with something that might work for you, in <code>sed</code>:</p> <pre><code>/Tracer/ { N; /interesting/ { h; :a; n; H; /^$/! ba; g; p } } </code></pre> <p>An explanation follows!</p> <p>Given a test file like this:</p> <pre><code>boring boring awesome [Tracer] interesting totally interesting and awesome still interesting very interesting back to boring awesome [Tracer] Nah just kidding nope darn </code></pre> <p>The above command expands to the following, with explanation:</p> <pre><code>/Tracer/ { # Looks for Tracer N # Moves on to the next line /interesting/ { # Looks to see if "interesting" is in the next line h # Put first two lines in hold space :a # Label "a" n # Move on to next line H # Appends line to hold space /^$/! ba # If not a blank line, branch back to "a" g # Put the hold space into the pattern space p # Print the pattern space } } </code></pre> <p>And, should do it like so:</p> <pre><code>sed -n '/Tracer/ { h; n; /interesting/ { :a; H; n; /^$/! ba }; g; p }' file.txt awesome [Tracer] interesting totally interesting and awesome still interesting very interesting [blank line] </code></pre> <p>Where <code>[blank line]</code> is a literal blank line, but I can't express that in a code block here. Obviously you can modify the regexes for <code>/Tracer/</code> and <code>/interesting/</code> easily enough, depending on the specific contents of the lines.</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