Count number of digits in a word


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Count number of digits in a word
# 1  
Old 01-05-2009
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 Smilie
# 2  
Old 01-05-2009
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  
Old 01-05-2009
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  
Old 01-05-2009
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  
Old 01-05-2009
Quote:
Originally Posted by ./hari.sh
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 Smilie
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  
Old 01-05-2009
Code:
#!/bin/ksh

WORD=abcd1234

[[ $(echo "${WORD}" | awk '{print gsub("[0-9]", "")}')  -eq 5 ]] && echo OK || echo KO

# 7  
Old 01-05-2009
Or:
Code:
% perl -le'print shift=~/\d{5}$/?"OK":"KO"' abcd1234
KO
% perl -le'print shift=~/\d{5}$/?"OK":"KO"' abcd12345
OK

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

UNIX script to check word count of each word in file

I am trying to figure out to find word count of each word from my file sample file hi how are you hi are you ok sample out put hi 1 how 1 are 1 you 1 hi 1 are 1 you 1 ok 1 wc -l filename is not helping , i think we will have to split the lines and count and then print and also... (4 Replies)
Discussion started by: mirwasim
4 Replies

2. Shell Programming and Scripting

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 (5 Replies)
Discussion started by: ashwin3086
5 Replies

3. UNIX for Dummies Questions & Answers

how to count number of times each word exist in a file

I'm trying to count the number of times each word in the file exist for example if the file has: today I have a lot to write, but I will not go for it. The main thing is that today I am looking for a way to get each word in this file with a word count after it specifying that this word has... (4 Replies)
Discussion started by: shnkool
4 Replies

4. Shell Programming and Scripting

number of digits after decimal

Hi All, I have a file of decimal numbers, cat file1.txt 1.1382666907 1.2603107334 1.6118799297 24.4995857056 494.7632588468 560.7633734425 ..... I want to see the output as only 7 digits after decimal (5 Replies)
Discussion started by: senayasma
5 Replies

5. Shell Programming and Scripting

Need script to count specific word and iterate over number of files.

Hi Experts, I want to know the count of specific word in a file. I have almost 600+ files. So I want to loop thru each file and get the count of the specific word. Please help me on achieving this... Many thanks (2 Replies)
Discussion started by: elamurugu
2 Replies

6. Shell Programming and Scripting

Count the number of occurrences of the word

I am a newbie in UNIX shell script and seeking help on this UNIX function. Please give me a hand. Thanks. I have a large file. Named as 'MyFile'. It was tab-delmited. I am told to write a shell function that counts the number of occurrences of the ord “mysring” in the file 'MyFile'. (1 Reply)
Discussion started by: duke0001
1 Replies

7. UNIX for Dummies Questions & Answers

Count number of occurences of a word

I want to count the number of occurences of say "200" in a file but that file also contains various stuff including dtaes like 2007 or smtg like 200.1 so count i am getting by doing grep -c "word" file is wrong Please help!!!!! (8 Replies)
Discussion started by: shikhakaul
8 Replies

8. Shell Programming and Scripting

scripting - write a script that will count the number of times a particular word

hello everyone, I'm trying to learn some scripts but i cant get my head around two of them. 1. how can i write a script that will count the number of times a particular word is used in file? 2. how can i make a script that will take me to a web page from unix? if anyone could help it... (3 Replies)
Discussion started by: BigTool4u2
3 Replies

9. Shell Programming and Scripting

Count the number of occurence of perticular word from file

I want to count the number of occurence of perticular word from one text file. Please tell me "less" command is work in ksh or not. If it is not working then instead of that which command will work. :confused: (40 Replies)
Discussion started by: rinku
40 Replies

10. UNIX for Dummies Questions & Answers

word count showing wrong number of lines

Hi , I am using SUN OS Version 5.6. I have a file that contains records of length 270. when I do 'set nu' in vi editor, I get the count as 86. whereas when I do "wc -l" on the command prompt, it shows the count as only 85. this is very strange. why would the 'wc' show 1 record less. The job... (3 Replies)
Discussion started by: tselvanin
3 Replies
Login or Register to Ask a Question