- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - HP-UX
- >
- count the number of blank spaces in a string using...
Categories
Company
Local Language
Forums
Discussions
Forums
- Data Protection and Retention
- Entry Storage Systems
- Legacy
- Midrange and Enterprise Storage
- Storage Networking
- HPE Nimble Storage
Discussions
Forums
Discussions
Discussions
Discussions
Forums
Discussions
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
- BladeSystem Infrastructure and Application Solutions
- Appliance Servers
- Alpha Servers
- BackOffice Products
- Internet Products
- HPE 9000 and HPE e3000 Servers
- Networking
- Netservers
- Secure OS Software for Linux
- Server Management (Insight Manager 7)
- Windows Server 2003
- Operating System - Tru64 Unix
- ProLiant Deployment and Provisioning
- Linux-Based Community / Regional
- Microsoft System Center Integration
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Community
Resources
Forums
Blogs
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-29-2003 06:48 AM
07-29-2003 06:48 AM
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.
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-29-2003 06:56 AM
07-29-2003 06:56 AM
Re: count the number of blank spaces in a string using ksh script
mark
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-29-2003 07:07 AM
07-29-2003 07:07 AM
Re: count the number of blank spaces in a string using ksh script
# echo $var1
a b c d e f
# expr length "$var1" - length `echo "$var1" | sed "s/ //g"`
5
#
live free or die
harry
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-29-2003 08:36 AM
07-29-2003 08:36 AM
Re: count the number of blank spaces in a string using ksh script
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-29-2003 08:39 AM
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-29-2003 08:48 AM
07-29-2003 08:48 AM
Re: count the number of blank spaces in a string using ksh script
I just took your example in the 1st place.
Rgds,
Jean-Luc
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-29-2003 08:58 AM
07-29-2003 08:58 AM
Re: count the number of blank spaces in a string using ksh script
Thanks all for your very prompt replies.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-29-2003 08:40 PM
07-29-2003 08:40 PM
Re: count the number of blank spaces in a string using ksh script
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