Solved

how to count substring in a string

Posted on 2004-04-27
5
786 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

[Webinar] Code, Load, and Grow

Managing multiple websites, servers, applications, and security on a daily basis? Join us for a webinar on May 25th to learn how to simplify administration and management of virtual hosts for IT admins, create a secure environment, and deploy code more effectively and frequently.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
usage of %{name} in rpm spec file 4 58
Embeded Linux on Router 9 112
what is this linux system is? 16 123
improve Shell script 5 158
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 fix the unknown display problem in Linux Mint operating system. After installing the OS if you see Display monitor is not recognized then we can install "MESA" utilities to fix this problem or we can install additio…
Are you ready to implement Active Directory best practices without reading 300+ pages? You're in luck. In this webinar hosted by Skyport Systems, you gain insight into Microsoft's latest comprehensive guide, with tips on the best and easiest way…

738 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