![]() |
|
|
|
|
|||||||
| Forums | Portal | Register | Forum Rules | FAQ | Contribute | Members List | Arcade | Search | Today's Posts | Mark Forums Read |
| 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 !! |
|
|
||||
| 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 |
|
|
Submit Tools | LinkBack | Thread Tools | Search this Thread | Display Modes |
|
#1
|
|||
|
|||
|
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
|
|||
|
|||
|
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
|
|||
|
|||
|
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
|
|||
|
|||
|
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 |