Operating System - HP-UX
1833758 Members
2781 Online
110063 Solutions
New Discussion

count the number of blank spaces in a string using ksh script

 
SOLVED
Go to solution
Randy Goss
Occasional Contributor

count the number of blank spaces in a string using ksh script

I am trying to count the number of blank spaces in a string. The following command should work (if I'm reading the ManPages correctly):

var1="a b c d e f"
expr " " : "$var1"

all I get back is a zero instead of 5.


I also tried:

var1="abcdef"
expr c : $var1

in case there's some issue with processing blanks but, this returns a zero as well.


"expr" loosks pretty straight forward, except I can't get it to work.





7 REPLIES 7
Mark Greene_1
Honored Contributor

Re: count the number of blank spaces in a string using ksh script

Special characters, including spaces have to be quoted. Try sed or tr.

mark
the future will be a lot like now, only later
harry d brown jr
Honored Contributor

Re: count the number of blank spaces in a string using ksh script

Try:

# echo $var1
a b c d e f
# expr length "$var1" - length `echo "$var1" | sed "s/ //g"`
5
#


live free or die
harry
Live Free or Die
Elena Leontieva
Esteemed Contributor

Re: count the number of blank spaces in a string using ksh script

Randy,

You may alos do it like this:

coc462(root):/> var="a b c d e f"
coc462(root):/> echo $var
a b c d e f
coc462(root):/> expr `echo $var |tr -d "\012" |wc -c` - `echo $var |wc -w`
5

Elena.
Jean-Luc Oudart
Honored Contributor
Solution

Re: count the number of blank spaces in a string using ksh script

with awk

echo $var1 | awk '{print split($0,tab," ") -1;}'

Rgds,
Jean-Luc
fiat lux
Jean-Luc Oudart
Honored Contributor

Re: count the number of blank spaces in a string using ksh script

Sorry, but my awk script is cot correct if you start or end with a space.
I just took your example in the 1st place.

Rgds,
Jean-Luc
fiat lux
Randy Goss
Occasional Contributor

Re: count the number of blank spaces in a string using ksh script

Thanks. While waiting for the replies to my post I stumbled across the "tr" solution. However, I've switched to Jean-Luc's "awk".

Thanks all for your very prompt replies.
Suhas_2
Regular Advisor

Re: count the number of blank spaces in a string using ksh script

Hi Randy,
Try this ...

***************************************
#!/bin/ksh
#count_space.ksh : to count spaces.
var1=`echo "$1" | sed 's/ /_/g'`
echo $1 | wc -w | read WORDS
COUNT=`expr $WORDS - 1 `
echo $var1 | grep '^_' | wc -l | read a
if [ $a -gt 0 ]
then
COUNT=`expr $COUNT + 1 `
fi
echo $var1 | grep '_$' | wc -l | read b
if [ $b -gt 0 ]
then
COUNT=`expr $COUNT + 1 `
fi
echo "$COUNT"
*****************************************

Only thing is you need to pass the variable in double quotes.

suhas$ ./count_space.ksh "iijhhyn unm un nghy"
3
suhas$ ./count_space.ksh " iijhhyn unm un nghy "
5
suhas$ ./count_space.ksh "iijhhyn unm un nghy "
4
suhas$ ./count_space.ksh " iijhhyn unm un nghy"
4

***********************************

Hope this helps'

Keep forumming !!!
Suhas
Never say "Die"