Professional Web Applications Themes

number of occurences of a string in a line - Linux / Unix Administration

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...

  1. #1

    Default number of occurences of a string in a line

    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

    kr.badrinath@gmail.com Guest

  2. #2

    Default Re: number of occurences of a string in a line

    On 14 Nov 2005 11:44:24 -0800, com <com> wrote: 

    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.


    Dave Guest

  3. #3

    Default Re: number of occurences of a string in a line

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

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

    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 ;-)
    Loki Guest

  4. #4

    Default Re: number of occurences of a string in a line

    On 2005-11-14, com wrote: 

    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
    Chris Guest

Similar Threads

  1. Inserting Line Number
    By Raghu Murthy in forum PERL Beginners
    Replies: 1
    Last Post: November 9th, 10:49 PM
  2. Replies: 2
    Last Post: September 26th, 09:45 AM
  3. Get number of line with error
    By \Emo in forum PHP Development
    Replies: 1
    Last Post: September 11th, 01:38 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139