title: Show all 4 digit numbers (but not the whole line and exclude special characters appended to number) using regex in bash
body: <p>I want to find and print all 4 digit numbers (but not the whole line) in a file using regex in bash.</p>
  <p>My sample file looks like this:</p>
  <pre><code>12
  123
  1234
  2345 foo foo foo 
  foo foo 3456 foo foo
  # 8912 foo foo foo foo
  #7654
  -8999
  \6478
  /9023
  $7654
  A3356
  8349B
  1439$
  1762\
  12345
  123456
  0000
  0001
  </code></pre>
  <p>I would like my output to include only 4 digits numbers (any number that has normal or special characters appended to it must be ignored):</p>
  <pre><code>1234
  2345
  3456
  8912
  0000
  0001
  </code></pre>
  <p>The closest I have been able to come to this is:</p>
  <pre><code>grep -E '(^|[^0-9])[0-9]{4}($|[^0-9])' file_with_numbers.txt
  </code></pre>
  <p>which errantly captures numbers with special characters appended and also prints the whole line when I only want the six values as shown above:</p>
  <pre><code>1234
  2345 foo foo foo foo
  foo foo 3456 foo foo
  # 8912 foo foo foo foo
  #7654
  -8999
  \6478
  /9023
  $7654
  A3356
  1439$
  1762\
  0000
  0001
  </code></pre>
  <p>Any suggestions on how I can get the exact desired output are appreciated. I am having trouble finding info for the appended special character exclusion as well as showing only the number and not the whole line.</p>