Find number of digits in a word

HI,

Can you tell me how to find the number of digits in a word.

 
$cat data.txt
+123456ad
87645768
 
Output should be 
6
8
awk '{for(i=1;i<=length($0);i++){if (substr($0,i,1)>=0 && substr($0,i,1)<=9) {++x}}{print x;x=0}}' data.txt
awk '{n=gsub(/[0-9]/,"&");print n}' data.txt
1 Like

Since lines is not printed, there is no need to use the "&"
This should work fine too.

awk '{n=gsub(/[0-9]/,x);print n}' data.txt

PS Nice idea on how to count the digit :slight_smile:

Simplifying further, there's also no need for n :

awk '{print gsub(/[0-9]/,x)}' data.txt

Regards,
Alister

This should work also:

awk '{print (NF-1)}' FS='[0-9]' data