Results 1 to 4 of 4
  1. #1
    Join Date
    Aug 2003
    Posts
    12

    Unanswered: Count of number of occurence of a character in a record

    Hi,

    As a novice to shell script, can someone pls. let me know as to how do I find the count of number of occurence of a particular character in a record.
    To be more specific, if I have 100 records in my file, I have to take 1 record at a time, in a loop, and find the count of number of occurence of double quotes (") in that record.
    Any help would be sincerely appreciated.

    Shyam.

  2. #2
    Join Date
    Dec 2003
    Posts
    6

    Re: Count of number of occurence of a character in a record

    Originally posted by shyams75
    Hi,

    As a novice to shell script, can someone pls. let me know as to how do I find the count of number of occurence of a particular character in a record.
    To be more specific, if I have 100 records in my file, I have to take 1 record at a time, in a loop, and find the count of number of occurence of double quotes (") in that record.
    Any help would be sincerely appreciated.

    Shyam.
    ***********************************
    INPUT="\"" ;
    TOTAL=0;
    char="";
    for WORD in `cat $1 `
    do
    LOOPCOUNT=1 ;
    char=` echo $WORD | cut -c $LOOPCOUNT-1 ` ;
    while [ "$char" ]
    do
    if [ "$char" = "$INPUT" ]; then
    TOTAL=` expr $TOTAL + 1 ` ;
    fi ;
    LOOPCOUNT=` expr $LOOPCOUNT + 1 ` ;
    char=` echo $WORD | cut -c $LOOPCOUNT-$LOOPCOUNT ` ;
    done ;
    done ;
    print "THE TOTAL COUNT IS : $TOTAL " ;

    ***********************************

    Hope that the above will work for you,
    All you need to do is embed this in ur script or call it separately after putting each record in a file.


    Hanuman.

  3. #3
    Join Date
    Dec 2003
    Posts
    6

    Re: Count of number of occurence of a character in a record

    Originally posted by shyams75
    Hi,

    As a novice to shell script, can someone pls. let me know as to how do I find the count of number of occurence of a particular character in a record.
    To be more specific, if I have 100 records in my file, I have to take 1 record at a time, in a loop, and find the count of number of occurence of double quotes (") in that record.
    Any help would be sincerely appreciated.

    Shyam.
    ******************************
    sed 's/\"/ \
    \ ABCD1234 \
    \ /g' $1 | grep "ABCD1234" | wc -l
    ******************************

    The above is a cute one.

    Hanuman.

  4. #4
    Join Date
    Jun 2002
    Location
    UK
    Posts
    525
    This would do the job too...

    awk '{gsub (/[^"]/,"",$0); print "Record",NR":",length($0)}' yourFile

    (Substitute all non-quote characters in the record with an empty string, then print the record number and its new length.)

Posting Permissions

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