Solved

how to count substring in a string

Posted on 2004-04-27
5
775 Views
Last Modified: 2008-01-09
hello
Can anybody tell me how to count number of string in a string.i m a newbie to shell programming so dont know much about it.
Thanks
Amrita
0
Comment
Question by:bhatia_amrita
  • 2
  • 2
5 Comments
 
LVL 45

Expert Comment

by:sunnycoder
Comment Utility
Hi bhatia_amrita,

echo $string | awk -F "substring" ' { print NF-1 } '

Sunnycoder
0
 
LVL 8

Expert Comment

by:da99rmd
Comment Utility
Do you want the number of strings in the string like this?
"hi world how you doing" = 5
This you cont like this
echo $string | awk ' { print NF} '

The thing sonnycoder told you about will count the nr of times a certain string is find in a string like this
find "you" in
"Hi you bastard you" = 2
echo $string | awk -F "you" ' { print NF-1 } '
The -F changes the field separator to the word you and the NF is the number of fields.

Here is an awk howto is a good program for handling strings and files separated by anything.
http://www.cs.uu.nl/docs/vakken/st/nawk/nawk_toc.html

and here is a good bash scripting manual
http://www.dei.isep.ipp.pt/so1/ftpdei/sop1/Docs/Linux/abs-guide/

and here a sed manual a stream editor:
http://gnu.intissite.com/software/sed/manual/html_chapter/sed_toc.html

/Rob




0
 

Author Comment

by:bhatia_amrita
Comment Utility
me actually looking for counting number of occurences of a  given substring in a string
0
 
LVL 45

Accepted Solution

by:
sunnycoder earned 50 total points
Comment Utility
http:#10935603  tells exactly that
0
 

Author Comment

by:bhatia_amrita
Comment Utility
thnx a ton :)
0

Featured Post

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Have you ever been frustrated by having to click seven times in order to retrieve a small bit of information from the web, always the same seven clicks, scrolling down and down until you reach your target? When you know the benefits of the command l…
The purpose of this article is to demonstrate how we can upgrade Python from version 2.7.6 to Python 2.7.10 on the Linux Mint operating system. I am using an Oracle Virtual Box where I have installed Linux Mint operating system version 17.2. Once yo…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now