• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 815
  • Last Modified:

how to count substring in a string

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
bhatia_amrita
Asked:
bhatia_amrita
  • 2
  • 2
1 Solution
 
sunnycoderCommented:
Hi bhatia_amrita,

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

Sunnycoder
0
 
da99rmdCommented:
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
 
bhatia_amritaAuthor Commented:
me actually looking for counting number of occurences of a  given substring in a string
0
 
sunnycoderCommented:
http:#10935603  tells exactly that
0
 
bhatia_amritaAuthor Commented:
thnx a ton :)
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

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.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now