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 ...)
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.
I don't know what shell you're using, but this works in ksh93:
$ echo "this (by that I mean this) is a \"paren\": )" | ./count.ksh
There were 3 characters found!
$
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:
But the third column is the important one, so I would go with:
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()
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;
}
}
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):
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.
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)
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)
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)
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)
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)
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)