title: sed - Get only the replaced string from a multiline input & omit unmatched lines!
body: <p><strong>I want</strong> sed to omit all non-matching lines, and only output the replaced string (of the single/multiple intended line/s).</p>
  
  <p><strong>In other words:</strong> I've got a hay stack, and only want the needle returned, not all the hay which was searched and which remained unaltered.</p>
  
  <p><strong>Or again in other words:</strong> Search/replace a RegEx described string in a multi line string, and only get that string returned. (As it is possible with the PHP function <a href="http://www.php.net/manual/en/function.preg-replace.php" rel="noreferrer">http://www.php.net/manual/en/function.preg-replace.php</a> )</p>
  
  <p><strong>My current workaround</strong> is to first filter with grep, and then pipe only the matching lines into sed for replacement:</p>
  
  <pre><code>echo -e "Bla\nBla\nImportant1: One \nBla\nImportant2: Two\nBla\nBla" | egrep "^Important1.*$" | sed -E "s/^Important1: *\b(.*)\b */\1/g"
  # From the multiple line input I only want the "One One" (with pre/post whitespace removed, hence matching the word boundaries with "\b")
  # And I want no "Bla bla" lines in the result!
  </code></pre>
  
  <p>But I'd like to have a <strong>single solution within sed</strong>. Or is this out of intended sed usage, and should I therefore better use something else? Btw, issue: <a href="https://stackoverflow.com/questions/2807033">multiline sed using backreferences</a> seemed somehow related, but I am not sure!</p>