The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

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



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Count number of occurences of a word shikhakaul UNIX for Dummies Questions & Answers 8 06-25-2009 11:10 AM
PERL: Simple reg expr validate 6 digits number BufferExploder Shell Programming and Scripting 2 09-10-2008 11:15 AM
Count the number of occurence of perticular word from file rinku Shell Programming and Scripting 40 08-10-2007 07:33 PM
word count showing wrong number of lines tselvanin UNIX for Dummies Questions & Answers 3 01-06-2004 11:33 PM
restrain the number of digits of a PID mlefebvr UNIX for Advanced & Expert Users 1 05-27-2002 09:33 AM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish
 
LinkBack Thread Tools Search this Thread Rating: Thread Rating: 1 votes, 4.00 average. Display Modes
  #1 (permalink)  
Old 01-05-2009
./hari.sh's Avatar
./hari.sh ./hari.sh is offline
Registered User
  
 

Join Date: Jul 2007
Location: India
Posts: 43
Count number of digits in a word

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
  #2 (permalink)  
Old 01-05-2009
tsurko tsurko is offline
Registered User
  
 

Join Date: Jun 2007
Location: Sofia, Bulgaria
Posts: 35
Could you show us how you use this code in your script? If you want to save it in a variable try something like this:
Code:
COUNT=`echo $WORD | grep -oE [[:digit:]] | wc -l`
  #3 (permalink)  
Old 01-05-2009
./hari.sh's Avatar
./hari.sh ./hari.sh is offline
Registered User
  
 

Join Date: Jul 2007
Location: India
Posts: 43
there is some other prob...

Let me tell you what exactly I want

I have a word having 8 chrs, something like abc12345 or ab123456

Now, if last five chrs are digits then I wanna do some other operation

I used the code
Code:

if [ `echo $WORD | cut -c 4-8  | grep -oE [[:digit:]] | wc -l` -eq 5 ]
then
do this...
bash command line it gives the output, but in script it gives "0" always"
  #4 (permalink)  
Old 01-05-2009
SFNYC SFNYC is offline
Registered User
  
 

Join Date: Jun 2008
Location: New York City
Posts: 91
Code:
$ cat ./testgrep.ksh
#!/bin/ksh

WORD=abc12345

echo $WORD|grep -qE "[[:digit:]]{5}$"
if [ $? -eq 0 ]; then
     echo "Do this with $WORD"
else
     echo "Else do this with $WORD"
fi

WORD2=ab123456

echo $WORD2|grep -qE "[[:digit:]]{5}$"
if [ $? -eq 0 ]; then
     echo "Do this with $WORD2"
else
     echo "Else do this with $WORD2"
fi

WORD3=abc1234

echo $WORD3|grep -qE "[[:digit:]]{5}$"
if [ $? -eq 0 ]; then
     echo "Do this with $WORD3"
else
     echo "Else do this with $WORD3"
fi

exit 0

$ ./testgrep.ksh
Do this with abc12345
Do this with ab123456
Else do this with abc1234
  #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,793
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
  #6 (permalink)  
Old 01-05-2009
vgersh99's Avatar
vgersh99 vgersh99 is online now Forum Staff  
Moderator
  
 

Join Date: Feb 2005
Location: Boston, MA
Posts: 5,116
Code:
#!/bin/ksh

WORD=abcd1234

[[ $(echo "${WORD}" | awk '{print gsub("[0-9]", "")}')  -eq 5 ]] && echo OK || echo KO
  #7 (permalink)  
Old 01-05-2009
radoulov's Avatar
radoulov radoulov is offline Forum Staff  
addict
  
 

Join Date: Jan 2007
Location: Варна, България / Milano, Italia
Posts: 2,793
Or:
Code:
% perl -le'print shift=~/\d{5}$/?"OK":"KO"' abcd1234
KO
% perl -le'print shift=~/\d{5}$/?"OK":"KO"' abcd12345
OK
Sponsored Links
Closed Thread

Bookmarks

Tags
perl, perl shift, shift, shift perl

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT -4. The time now is 04:47 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language translation by Google.
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0