Unix Shell - number of occurences of a string in a line

This is Interesting: Free IT Magazines  
Home > Archive > Unix Shell > November 2005 > number of occurences of a string in a line





You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

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

2005-11-14, 5:56 pm

Hi,

I am very new to unix ..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 for your time.

Michael Heiming

2005-11-14, 5:56 pm

In comp.unix.shell kr.badrinath@gmail.com:
> Hi,


> I am very new to unix ..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 for your time.


Please don't forget to add the date your homework is due, so
people know how much time they have?

--
Michael Heiming (X-PGP-Sig > GPG-Key ID: EDD27B94)
mail: echo zvpunry@urvzvat.qr | PERL -pe 'y/a-z/n-za-m/'
#bofh excuse 120: we just switched to FDDI.
Ed Morton

2005-11-14, 5:56 pm

kr.badrinath@gmail.com wrote:
> Hi,
>
> I am very new to unix ..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



ITYM line3:2occurences

>
> Thx for your time.
>


awk -F"siva" '{printf "line%d:%doccurences\n",NR,NF-1}'

Regards,

Ed.
Rakesh Sharma

2005-11-14, 5:56 pm


kr.badrinath@gmail.com wrote:
> Hi,
>
> I am very new to unix ..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
>
>


perl -lne'print"Line",$.,":",s/siva//g,"occurences"' input_file

Sponsored Links






Free braindumps | Software forum | Database administration forum

Copyright 2003 - 2008 webservertalk.com