The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com




View Single Post in the UNIX and Linux Forums - Click on the Thread or Permalink to View Entire Thread -->
  #5 (permalink)  
Old 01-05-2009
radoulov's Avatar
radoulov radoulov is offline Forum Staff  
addict
  
 

Join Date: Jan 2007
Location: Варна, България / Milano, Italia
Posts: 2,928
Quote:
Originally Posted by ./hari.sh View Post
Hi all

Can anybody suggest me, how to get the count of digits in a word

I tried


Code:
WORD=abcd1234
echo $WORD | grep -oE [[:digit:]] | wc -l
4

It works in bash command line, but not in scripts
It seems you're on GNU system, so you probably have bash >= 3 and you could try something like this:

Code:
$ WORD=abcd1234
$ [[ $WORD  =~ [0-9]{5}$ ]] && echo OK || echo KO
KO
$ WORD=abcd12345
$ [[ $WORD  =~ [0-9]{5}$ ]] && echo OK || echo KO
OK