If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Data Access, Manipulation & Batch Languages > Unix Shell Scripts > Count of number of occurence of a character in a record

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 12-16-03, 06:09
shyams75 shyams75 is offline
Registered User
 
Join Date: Aug 2003
Posts: 12
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.
Reply With Quote
  #2 (permalink)  
Old 12-30-03, 21:28
hanuman hanuman is offline
Registered User
 
Join Date: Dec 2003
Posts: 6
Re: Count of number of occurence of a character in a record

Quote:
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.
Reply With Quote
  #3 (permalink)  
Old 12-31-03, 01:46
hanuman hanuman is offline
Registered User
 
Join Date: Dec 2003
Posts: 6
Re: Count of number of occurence of a character in a record

Quote:
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.
Reply With Quote
  #4 (permalink)  
Old 12-31-03, 05:22
Damian Ibbotson Damian Ibbotson is offline
Padawan
 
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.)
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On