 <p>This should do it:</p> <pre><code>perl -ne '/ZTFN00\D+(\d+)/ &amp;&amp; print $1,"\n"' yourfile </code></pre> <p>If your 'number' is always separated by whitespace ( where there 00 after ZTFN isn't) then you can use that as your test:</p> <pre><code>perl -ne 'print m/\b(\d+)\b/,"\n"' yourfile </code></pre>