number of occurences of a string in a line
Web Server forum
Back To The Forum Home!Search!Private Messaging System

Web Server Talk Web Server Talk > Unix and Linux reviews > Free Unix support > Unix administration > number of occurences of a string in a line




  Last Thread   Next Thread Next
  Show Printable Version Email this Page Subscribe to this Thread      Post New Thread    Post A Reply      

    number of occurences of a string in a line  
kr.badrinath@gmail.com


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
11-14-05 10:57 PM

Could you please help me out to find number of occurences of  a string
in a line.

input file:
123siva456siva789siva
222siva555siva
888siva 999siva

expected output for word siva:

line1:3occurences
line2:2occurences
line3:3occurences

Thx,
Badri






[ Post a follow-up to this message ]



    Re: number of occurences of a string in a line  
Dave Hinz


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
11-14-05 10:57 PM

On 14 Nov 2005 11:44:24 -0800, kr.badrinath@gmail.com <kr.badrinath@gmail.com> wrote:
> Could you please help me out to find number of occurences of  a string
> in a line.

When is your homework due, please, and should we expect to see _all_ of
your assignments?  If so, please post them in a batch for the
convenience of the people who feel like doing your work for you.







[ Post a follow-up to this message ]



    Re: number of occurences of a string in a line  
Loki Harfagr


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
11-19-05 10:51 PM

Le Mon, 14 Nov 2005 11:44:24 -0800, kr.badrinath a écrit_:

> Could you please help me out to find number of occurences of  a string in
> a line.
>
> input file:
> 123siva456siva789siva
> 222siva555siva
> 888siva 999siva
>
> expected output for word siva:
>
> line1:3occurences
> line2:2occurences
> line3:3occurences

Are you really sure you actually count 3 occurences
of 'siva' in line 3 ?

>
> Thx,
> Badri

Anyway, if you want to try this, it'd help you a bit in
reading a few pages as to explain to your trainor how
you made it up :

$ awk 'NF{print NF-1}' FS='siva' yourinputfile

This, on a little bit extended test file gives :

$ cat MISCFILES/countwordsinline.txt
123siva456siva789siva
222siva555siva
888siva 999siva
siva
888siva 999siva111visa
888siva 999siva111visa333

$ gawk 'NF{print NF-1}' FS='siva' MISCFILES/countwordsinline.txt
3
2
2
1
2
2

The exercise of printing out a 0 when necessary is left as
a bait for reading some pages ;-)





[ Post a follow-up to this message ]



    Re: number of occurences of a string in a line  
Chris F.A. Johnson


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
11-19-05 10:51 PM

On 2005-11-14, kr.badrinath@gmail.com wrote:
> Could you please help me out to find number of occurences of  a string
> in a line.
>
> input file:
> 123siva456siva789siva
> 222siva555siva
> 888siva 999siva
>
> expected output for word siva:
>
> line1:3occurences
> line2:2occurences
> line3:3occurences

awk '{ l = $0
r = gsub("siva","")
printf "line %d: %d occurrences\n", NR, r
}'

--
Chris F.A. Johnson, author   |    <http://cfaj.freeshell.org>
Shell Scripting Recipes:     |  My code in this post, if any,
A Problem-Solution Approach  |          is released under the
2005, Apress                 |     GNU General Public Licence





[ Post a follow-up to this message ]



    Sponsored Links  




 





   All times are GMT. The time now is 03:45 AM.      Post New Thread    Post A Reply      
  Last Thread   Next Thread Next


Most Popular forums 

Forum Jump:
Rate This Thread:

Forum Rules:
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is OFF
vB code is ON
Smilies are ON
[IMG] code is OFF
 
Medical and Health forum | Computer Games Reviews | Graphics design forum

Back To The Top
Home | Usercp | Faq | Register