开发者

Search for a number in each line of a string in ksh and nawk

The input file:

vnic10
e1000g1
e1000g2
vnic10
blablabla888blablablabla999blabla

Output needed:(Only the numbers开发者_如何学JAVA in each line)

10
1000 1
1000 2
10
888 999

We can do this using sed and remembered patterns. I am looking for the logic to get this done using awk/nawk and ksh commands.


Not the best formatting, but tr does the job

$ tr '[a-z]' ' ' < file_containing_input
    10
 1000 1
 1000 2
    10
         888            999 

Using awk:

$ awk '{ gsub(/[a-z]+/, " "); print }' file_containing_input
 10
 1000 1
 1000 2
 10
 888 999 

And one in bash (now I need to stop...)

$ while read a; do echo ${a//[a-z]/ }; done < file_containing_input
10
1000 1
1000 2
10
888 999


Seems like a homework, as "we can do this using sed". Actually it is simple substitute command, that substitutes any amount of non numeric chars with space.

sed -r "s/[^0-9]+/\ /g" input_file
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜