Solved

how to count substring in a string

Posted on 2004-04-27
5
783 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
ID: 10935603
Hi bhatia_amrita,

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

Sunnycoder
0
 
LVL 8

Expert Comment

by:da99rmd
ID: 10936547
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
ID: 10941582
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
ID: 10946017
http:#10935603  tells exactly that
0
 

Author Comment

by:bhatia_amrita
ID: 10946703
thnx a ton :)
0

Featured Post

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

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…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…

685 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