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


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Looking for a single line to count how many times one character occurs in a word...
# 1  
Old 06-12-2009
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
# 2  
Old 06-12-2009
awk might help
Code:
echo "$string"|awk -F"-" '{print NF-1}'
/home> echo "-12-34-56-2-4--56"|awk -F"-" '{print NF-1}'
7
/home>

# 3  
Old 06-12-2009
In how many directions do I need to face to thank you!!

Quote:
Originally Posted by vidyadhar85
awk might help
Code:
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

Last edited by Shingoshi; 06-12-2009 at 11:59 PM.. Reason: Providing proof of the working code.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Get count of multiple word in single command

Hello Experts, I have a log file that contains 4 different type of exception : 1- Exception 2- Fatal 3- Error 4- Exec My requirement is to find count of each type of exception, i tried using combination of -E and -C but that doesn't seems to be working : grep -ec 'Exception' -ec... (4 Replies)
Discussion started by: mukulverma2408
4 Replies

2. Shell Programming and Scripting

Find word in a line and output in which line the word occurs / no. of times it occurred

I have a file: file.txt, which contains the following data in it. This is a file, my name is Karl, what is this process, karl is karl junior, file is a test file, file's name is file.txt My name is not Karl, my name is Karl Joey What is your name? Do you know your name and... (3 Replies)
Discussion started by: anuragpgtgerman
3 Replies

3. Shell Programming and Scripting

Count same word which has come many times in single lines & pars

can i get a simple script for , Count same word which has come many times in single lines & pars Eg file would be == "Thanks heman thanks thanks Thanks heman thanks man" So resullt should be Thanks = 5 heman=2 man = 1 thanks in advance :) Please use code tags for code and... (1 Reply)
Discussion started by: heman96
1 Replies

4. UNIX for Dummies Questions & Answers

How do I count how many times a specific word appear in a file (ksh)?

Hi Please can you help how do I count the number of specific characters or words that appear in a file? (8 Replies)
Discussion started by: fretagi
8 Replies

5. UNIX for Dummies Questions & Answers

[Solved] Count amount of times of appearing of character before a word?

Hello Is there a way to calculate how many times a particular symbol appeared in a string before a particular word. Desktop/Myfiles/pet/dog/puppy So, I want to count number of occurence of"/" in this directory before the word dog lets say. Cheers, Bob (3 Replies)
Discussion started by: FUTURE_EINSTEIN
3 Replies

6. UNIX for Dummies Questions & Answers

how to count number of times each word exist in a file

I'm trying to count the number of times each word in the file exist for example if the file has: today I have a lot to write, but I will not go for it. The main thing is that today I am looking for a way to get each word in this file with a word count after it specifying that this word has... (4 Replies)
Discussion started by: shnkool
4 Replies

7. UNIX for Advanced & Expert Users

Count specific word or character per line

Hi, I need help regarding counting specific word or character per line and validate it against a specific number i.e 10. And if number of character equals the specific number then that line will be part of the output. Specific number = 6 Specific word or char = || Sample data:... (1 Reply)
Discussion started by: janzper
1 Replies

8. Shell Programming and Scripting

scripting - write a script that will count the number of times a particular word

hello everyone, I'm trying to learn some scripts but i cant get my head around two of them. 1. how can i write a script that will count the number of times a particular word is used in file? 2. how can i make a script that will take me to a web page from unix? if anyone could help it... (3 Replies)
Discussion started by: BigTool4u2
3 Replies

9. Shell Programming and Scripting

Need to replace the first word of a line if it occurs again in the next line(shell)

Hi folks, have a look into the attachment, i am not familiar with unix, can you please help me in this regard. thanks in advance, :) regards, Geeko (4 Replies)
Discussion started by: geeko
4 Replies

10. Shell Programming and Scripting

TO find the word which occurs maximum number of times

Hi Folks !!!!!!!!!!!!!!!!!!! My Requirement is............. i have a input file: 501,501.chan 502,502.anand 503,503.biji 504,504.raja 505,505.chan 506,506.anand 507,507.chan and my o/p should be chan->3 i.e. the word which occurs maximum number of times in a file should be... (5 Replies)
Discussion started by: aajan
5 Replies
Login or Register to Ask a Question