Computing.Net > Forums > Unix > Count no. of words of variable

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

Count no. of words of variable

Reply to Message Icon

Name: ahteck
Date: June 30, 2003 at 02:43:55 Pacific
OS: AIX UNIX
CPU/Ram: 512
Comment:

Hi,

How can I count no. of words for a variable?

var="abcxyz_0353.prt"

Then , I need the words count to be 15.

Please help.

ahteck



Sponsored Link
Ads by Google

Response Number 1
Name: Sorin Ploscaru
Date: June 30, 2003 at 02:51:48 Pacific
Reply:

Hi!

It's simple:

# var="dasdasdasd,qwdqwd"
# echo $var | wc -c
18
#

Regards,
Sorin


0

Response Number 2
Name: nails
Date: June 30, 2003 at 07:10:37 Pacific
Reply:

Hi:

I think you're asking for the length. In ksh/bash shells, use the length operator:

#untested
cnt=${#var}

Regards,

Nails


0

Response Number 3
Name: nails
Date: June 30, 2003 at 09:00:44 Pacific
Reply:

I forgot to mention, the expr command which should work with any shell:

cnt=`expr "$var" : ".*"`

Regards,

Nails


0

Sponsored Link
Ads by Google
Reply to Message Icon

Related Posts

See More


bash or any languages Reg. expression in string...



Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Unix Forum Home


Sponsored links

Ads by Google


Results for: Count no. of words of variable

Get the last few words of a line? www.computing.net/answers/unix/get-the-last-few-words-of-a-line/7459.html

return no. of lines from grep www.computing.net/answers/unix/return-no-of-lines-from-grep/6508.html

counting lines of a find command www.computing.net/answers/unix/counting-lines-of-a-find-command/6607.html