Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

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

 1
 2
 3
 5
 9
 10
 12
 1
 4

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
add comment

2 Answers

Try

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
add comment

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
1  
@jw013, removing the leading empty line if the first character of "input" is not a digit. –  Stephane Chazelas Nov 16 '12 at 16:27
add comment

Your Answer

 
discard

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.