title: Do not print unmatched text with sed
body: <p>I want to print only matched lines and strip unmatched ones, but with following:</p>
  
  <pre><code>$ echo test12 test  | sed -n 's/^.*12/**/p'
  </code></pre>
  
  <p>I always get:</p>
  
  <pre><code>** test
  </code></pre>
  
  <p>instead of:</p>
  
  <pre><code>**
  </code></pre>
  
  <p>What am I doing wrong?</p>
  
  <p>[edit1]</p>
  
  <p>I provide more information of what I need - and actually I should start with it. So, I have a command which produced lots of lines of output, I want to grab only parts of the lines - the ones that matches, and strip the result. So in the above example 12 was meant to find end of matched part of the line, and instead of ** I should have put &amp; which represents matched string. So the full example is:</p>
  
  <pre><code>echo test12 test  | sed -n 's/^.*12/&amp;/p'
  </code></pre>
  
  <p>which produces exactly the same output as input:</p>
  
  <pre><code>test12 test
  </code></pre>
  
  <p>the expected output is:</p>
  
  <pre><code>test12
  </code></pre>
  
  <p>As suggested I started to find a grep alternative and the following looks promising:</p>
  
  <pre><code>$ echo test12 test  | grep -Eo "^.*12"
  </code></pre>
  
  <p>but I dont see how to format the matched part, this only strips unmatched text.</p>