Counting patterns in a shell string


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Counting patterns in a shell string
# 1  
Old 06-02-2003
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
# 2  
Old 06-02-2003
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-03-2003 at 02:19 AM..
# 3  
Old 06-02-2003
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
Try this:

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

Regards,
Yeheya
 
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Counting a string between 2 strings...

I have been working on this for a little while and have been unable to come to a solution. Any help would be appreciated. I am working on a UNIX workstation and have a 30-40 meg text file that I am working with. In my real file there is hundreds of Jobs. Example of input file; misc logging data... (1 Reply)
Discussion started by: ny_evan
1 Replies

2. Shell Programming and Scripting

Counting Instances of a String with AWK

I have a list of URLs and I want to be able to count the number of instances of addresses ending in a certain TLD and output and sort it like so. 5 bdcc.com 48 zrtzr.com 49 rvo.com Input is as so ync.org sduzj.edu sduzj.edu sduzj.edu sduzj.edu sduzj.edu sduzj.edu sduzj.edu... (1 Reply)
Discussion started by: Pjstaab
1 Replies

3. UNIX for Dummies Questions & Answers

counting occurrence of characters in a string

Hello, I have a string like this 0:1:2:0:2:2:4:0:0:0:-200:500...... what i want is to break down how many different characters are there and their count. For example for above string it should display 0 - 5 times 1 - 1 times 2 - 3 times 4 - 1 times . . . I am stuck in writing... (8 Replies)
Discussion started by: exit86
8 Replies

4. UNIX for Dummies Questions & Answers

Help with counting string elements

Hi All, I hv several files which have hundreds of lines each for example>XYZ.abc01 NNNTCGGTNNNNNCCACACACMYACACACCCACACCCACSCARCAC I'd like to exculde the first line beginning with ">" and then for the rest of the lines get a count for each string element. So for the above example I would like... (8 Replies)
Discussion started by: pawannoel
8 Replies

5. Shell Programming and Scripting

need help in string patterns

Hi, i have following lines of code which is properly working. CAT1="${InputFile}CAT_*0?????" CAT2="${InputFile}CAT_*0?????" CountRecords(){ integer i=1 while ]; do print P$i `nawk 'END {print NR}' $1 ` >> ${OutputPath}result.txt & i=i+1 shift done } CountRecords "$CAT1"... (8 Replies)
Discussion started by: malikshahid85
8 Replies

6. Shell Programming and Scripting

Counting lines between two patterns

Hi Guys, I have a file as follows: wwe khfgv jfo wwe jhgfd wwe wwe hoaha hao lkahe wwe (13 Replies)
Discussion started by: npatwardhan
13 Replies

7. UNIX for Dummies Questions & Answers

grep -c script counting string twice instead of once?

I tried this script to get a count of the occurrence of a string in files. I have multiple files in one directory I will use this on. All the filenames begin "invALL.06" The script works, except it counts twice for every one instance of 'Invoice Total'. If there are 5 occurences of 'Invoice... (2 Replies)
Discussion started by: scarletsupra
2 Replies

8. Shell Programming and Scripting

Counting string of a variable

Hi, There is a variable f_name, it store some file names. Value of f_name=a.sql b.sql c.sql....... like this. want to count how many file name the var f_name stores. Without using loop is there any command to count that. (5 Replies)
Discussion started by: Dip
5 Replies

9. Shell Programming and Scripting

Counting the max length of string

Hi all, I have a flat file of 1000 rows. I want to check the length of the 5th column. The one having the longest length , I want to set it as DEFINED PARAMETER. So later I can check others with that particular number only. Any ideas ?? (2 Replies)
Discussion started by: ganesh123
2 Replies
Login or Register to Ask a Question