Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have a file with many lines, like

1 jfkdajfd   1 2 3 5
2 fkldfjld   
3 fdkfloaf   9 10
4 fldfldkf 
5 fdskf;ak   12 1 4

I want to get all the numbers and put them in a column in a file, like


how can I achieve this? thanks

share|improve this question
Are you actually looking for numbers or to limit the output to consecutive digits (i.e. positive integers)? For a more specific example, do you want 123.456 to show up as 1 decimal number or 2 integers or not at all? – jw013 Nov 16 '12 at 16:24
It seems you are doing some exercises because you have asked a series of these questions today. And this question seems to serve no practical purpose with your description. Why not try to figure them out by youself first if necessary. – Edw4rd Nov 16 '12 at 17:00
Also asked on StackOverflow – glenn jackman Nov 16 '12 at 23:35


awk '{ for (i = 1; i <= NF; i++) if ($i + 0 == $i) print $i }' < input > output

I use x + 0 == x as the test to see if x is a valid awk number, then print the original string. It's up to you to decide if you want awk numbers (this includes floating point, negative numbers, and on many implementations, hexadecimal numbers as well), or something different.

share|improve this answer

How about

tr -cs 0-9 '[\n*]' < input | grep . > output
share|improve this answer
Just curious, what's the point of the grep .? – jw013 Nov 16 '12 at 16:26
@jw013, removing the leading empty line if the first character of "input" is not a digit. – Stéphane Chazelas Nov 16 '12 at 16:27

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.