Looking for a single line to count how many times one character occurs in a word...

I've been looking on the internet, and haven't found anything simple enough to use in my code. All I want to do is count how many times "-" occurs in a string of characters (as a package name). It seems it should be very simple, and shouldn't require more than one line to accomplish.

And this is for a bash script. (It's all I know!)

Thank you!
Shingoshi

awk might help

echo "$string"|awk -F"-" '{print NF-1}'
/home> echo "-12-34-56-2-4--56"|awk -F"-" '{print NF-1}'
7
/home>

I pasted this into my script, only changing $string to ${name}, and of course it works. Wow! That's about all I can say. I'm going to write a few lines below, which may help someone else searching the internet find this solution.

1.) How do I count the number of times a single character is repeated in a string?
2.) Count any repeated characters.

If you or anyone else can think of better ways to get the attention of others for this, please post your own comment. I really can't be the only one who will ever need this. I really do need to learn AWK!

To be absolutely clear for everyone else. This is the important portion of the code string above: | awk -F"-" '{print NF-1}'

Shingoshi

Since I can't create a new separate post, I'll have to edit this one and hope that it get the attention this answer deserves. I asked this question so that I could retain only the package name without the version of any package listed in /var/log/packages (on Slackware).

Here's the working solution:
1.) PKG_LABEL="awn-extras-applets-0.2.6-x86_64-3gsb"
2.) There are always only 3 "-" from the end of the (Slackware) package string to the package name.
3.) I changed "awk -F"-" '{print NF-1}'" to "awk -F"-" '{print NF-3}'"
4.) The working code is:
chop=$(echo ${PKG_LABEL} | awk -F"-" '{print NF-3}')
echo "${PKG_LABEL}" | cut -d- -f-${chop}
5.) The result is now: awn-extras-applets

Shingoshi