counting characters


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting counting characters
# 1  
Old 02-18-2003
Network counting characters

Dears,

I would like to count the number of "(" and ")" that occur in a file.
(syntax checking script). I tried to use "grep -c" and this works fine as long as there is only one character (for which I do a search) on a line.
Has anyone an idea how I can count the number of specific characters in a file ?

(I prefer not to use awk or sed but if there's no other option ...)
# 2  
Old 02-18-2003
I don't understand your aversion to awk in this context because it's so simple:
Code:
echo '(a)(bc)d)' | awk '-F[()]' { t += NF - 1 } END { print t }'

produces "5" on the output.
# 3  
Old 02-19-2003
Thanks, works fine.
My aversion to awk or sed
1) I got a good book about it but, no team to practise/learn it
2) the people who will be using/changing my script don't know anything of awk/sed => if they want to change it they will always come to me.

But anyway I think I'll stick to awk cause it is indeed simple.
# 4  
Old 02-19-2003
I don't know what shell you're using, but this works in ksh93:
Code:
#! /bin/ksh

cnt=0
while read -n 1 char; do
        [[ "$char" == "(" || "$char" == ")" ]] && {
        ((cnt++))
        }
done

print There were $cnt characters found!

$ echo "this (by that I mean this) is a \"paren\": )" | ./count.ksh
There were 3 characters found!
$
# 5  
Old 02-24-2003
Code:
awk '{\
workline=$0
lcnt=gsub("\(","",workline)
rcnt=gsub("\)","",workline)
ocnt=ocnt+lcnt-rcnt
printf "%3d %3d %3d %-s\n",lcnt,rcnt,ocnt,$0
}' myscript

Sorry, another awk solution. This one prints the script being checked with each line preceded by 3 counts: left parens on the line, right parens on the line, and a running count of open parens after the line. Sample output:

1 1 0 function chkforEnd() {
2 2 0 if (match($0,"=$"))
0 0 0 SOstat="C"
0 0 0 else
0 0 0 SOstat="O"}

But the third column is the important one, so I would go with:

Code:
awk '{\
workline=$0
lcnt=gsub("\(","",workline)
rcnt=gsub("\)","",workline)
ocnt=ocnt+lcnt-rcnt
printf "%3d %-s\n",ocnt,$0
}' myscript

As long as paren sets open and close on the same line, the count stays zero. When paren sets remain open across lines, the count will go non-zero for those few lines. When I omit a right paren, the open count remains positive for remainder of script:

0 {if (phase==1)
0 if (SOstat=="C")
0 if (NF>0)
0 chkforEnd()
0 else
0 phase=2
0 else
1 if (NF>1 && match($1,"^[A-Z]")
1 {holdline=holdline "="
1 corrected++
1 chkforEnd()}
1 else
1 if (NF>0 && match($1,"^[0-9][0-9]*$"))
1 chkforEnd()

and when I omit the left paren instead of the right paren:

0 {if (phase==1)
0 if (SOstat=="C")
0 if (NF>0)
0 chkforEnd()
0 else
0 phase=2
0 else
-1 if NF>1 && match($1,"^[A-Z]"))
-1 {holdline=holdline "="
-1 corrected++
-1 chkforEnd()}
-1 else
-1 if (NF>0 && match($1,"^[0-9][0-9]*$"))
-1 chkforEnd()
Jimbo
# 6  
Old 02-25-2003
Hi,
Anyone interested in Perl?
Perl can do that faster than awk where we call it pattern matching.

When it match, it can count the character. Say ur filename is Myfile.txt

open (FILE, "<Myfile.txt")
{
while (<FILE>)
{
if /(\W+\sand\s\W+)/)
{
my $data = $1;
my $data1 =length ($data) -2;
#$data1 contains the value of the length of the word (" and ")
print $data1;
}
}
# 7  
Old 02-25-2003
Quote:
Originally posted by lcfoo
I think you misunderstood the problem: The OP wants to count parentheses, not occurrences of "and" between parentheses. One variation in awk might go like this (untested code):
Code:
#!/usr/bin/awk
BEGIN { t = 0 }
 { len1 = length
   gsub("[()]", "", $0)
   len2 = length
   t += len1 - len2
 }
END { print sum }

Translation into perl is left as an exercise for the reader. My original awk solution is simpler, and can be easily incorporated on a command line.
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Counting characters at each position

Hi All, here's a question from newbie I have a data like this, which set of small DNA sequences separated by new line GAATCCGGAAACAGCAACTTCAAANCA GTNATTCGGGCCAAACTGTCGAA TTNGGCAACTGTTAGAGCTCATGCGACA CCTGCTAAACGAGTTCGAGTTGAANGA TTNCGGAAGTGGTCGCTGGCACGG ACNTGCATGTACGGAGTGACGAAACCI... (6 Replies)
Discussion started by: amits22
6 Replies

2. Shell Programming and Scripting

Counting characters vertically

I do have a big file in the following format >A1 ATGCGG >A2 TCATGC >A3 -TGCTG The number of characters will be same under each subheader and only possible characters are A,T,G,C and - I want to count the number of A's, T's,G's, C's & -'s vertically for all the positions so that I... (5 Replies)
Discussion started by: Lucky Ali
5 Replies

3. Shell Programming and Scripting

Counting the number of characters

Hi all, Can someone help me in getting the following o/p I/p:... (7 Replies)
Discussion started by: Sri3001
7 Replies

4. Shell Programming and Scripting

Counting characters within a file

Ok say I wanted to count every Y in a data file. Then set Y as my delimiter so that I can separate my file by taking all the contents that occur BEFORE the first Y and store them in a variable so that I may use this content later on in my program. Then I could do the same thing with the next Y's... (5 Replies)
Discussion started by: puttster
5 Replies

5. Shell Programming and Scripting

taking characters and counting them

Nevermind, I figured out a way using the sed command. But I forget the basic way of counting characters within a variable :( (4 Replies)
Discussion started by: puttster
4 Replies

6. Shell Programming and Scripting

Counting characters with sed

Input: ghw//yw/hw///??u How can i count the slashes("/") using sed? (13 Replies)
Discussion started by: cola
13 Replies

7. Shell Programming and Scripting

counting characters

Hi All, I need some help in counting the number of letters in a big file with separations. Following is the file I have >AB_1 MLKKPIIIGVTGGSGGGKTSVSRAILDSFPNARIAMIQHDSYYKDQSHMSFEERVKTNYDHPLAFDTDFM IQQLKELLAGRPVDIPIYDYKKHTRSNTTFRQDPQDVIIVEGILVLEDERLRDLMDIKLFVDTDDDIRII... (6 Replies)
Discussion started by: Lucky Ali
6 Replies

8. UNIX for Dummies Questions & Answers

counting the occurence of particular characters

I want to list the occurence of particular characters in a line. my file looks like this a,b,c,d e,f,g h,y:e,g,y s f;g,s,w and I want to count how many commas are in each line so the file in the end looks like this: a,b,c,d 3 e,f,g 2 h,y:e,g,y s 3 f;g,s,w ... (2 Replies)
Discussion started by: Audra
2 Replies

9. Shell Programming and Scripting

Counting characters between comma's

I have a comma delimited file that roughly has 300 fields. Not all fields are populated. This file is fed into another system, what I need to do is count the amount of characters in each field and give me an output similiar to this: 1 - 6,2 - 25 The first number is the field and the second... (2 Replies)
Discussion started by: dbrundrett
2 Replies
Login or Register to Ask a Question