The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > UNIX for Dummies Questions & Answers
Google UNIX.COM


UNIX for Dummies Questions & Answers If you're not sure where to post a UNIX or Linux question, post it here. All UNIX and Linux newbies welcome !!

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Counting the number of occurances of all characters (a-z) in a string rsendhilmani Shell Programming and Scripting 5 08-05-2008 09:10 AM
matching 3 patterns in shell script saibsk UNIX for Dummies Questions & Answers 1 01-11-2008 12:06 PM
To extract the string between two patterns aajan Shell Programming and Scripting 6 09-16-2007 11:41 PM
count the number of files which have a search string, but counting the file only once sudheshnaiyer UNIX for Dummies Questions & Answers 1 08-11-2007 10:50 AM
Counting the max length of string ganesh123 Shell Programming and Scripting 2 02-23-2007 01:27 PM

Closed Thread
 
Submit Tools LinkBack Thread Tools Search this Thread Display Modes
  #1  
Old 06-02-2003
Registered User
 

Join Date: May 2003
Location: Australia
Posts: 7
Counting patterns in a shell string

Hello,

I am writing a shell script and I need to find a way to
count the number of whitespaces in a string.

Eg:
NAME="Bob Hope"
I am looking for a way to count the number of whitespaces in this string. So a command that would take this string and return 1.
Or take
"First Middle Last" and return 2.
I know this could be achieved using an array to store the NAME and then searching through each element of the array and count the number of whitespaces. But I was hoping that there would be a simplier way.

Thank You
Forum Sponsor
  #2  
Old 06-02-2003
Registered User
 

Join Date: May 2003
Location: Australia
Posts: 7
Don't worry. I found a solution.

NAME="Bob Hope"
spacecount=0
for((i=0; i<${#NAME};i++))
if test ${NAME:$i:1} = " "
then
let 'spacecount=spacesount+1'
fi
done

Just a simple loop

Last edited by kevin80; 06-02-2003 at 10:19 PM.
  #3  
Old 06-02-2003
Registered User
 

Join Date: Oct 2002
Posts: 676
kevin80,
why don't you post the solution so if someone has a similar need in the future, he can just search the forum.
  #4  
Old 06-03-2003
Registered User
 

Join Date: Feb 2002
Location: India
Posts: 36
Try this:

NAME="Bob Hope"
let v_space_count=`echo $NAME|awk 'BEGIN { RS=" " } END {print NR }'`-1
echo $v_space_count

Regards,
Yeheya
__________________
Yeheya Ansari
Software Engineer
Google The UNIX and Linux Forums
Closed Thread

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes




All times are GMT -7. The time now is 08:14 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008. All Rights Reserved.Ad Management by RedTyger Visit The Complex Event Processing Blog

Content Relevant URLs by vBSEO 3.2.0