counting the occurence of particular characters


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers counting the occurence of particular characters
# 1  
Old 03-28-2008
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

I need the lines to stay in tact because at the end I need to be able to print the line with the greatest number of commas

Thankyou in advance!
# 2  
Old 03-28-2008
Code:
perldoc -q count
Found in /usr/share/perl/5.8/pod/perlfaq4.pod
  How can I count the number of occurrences of a substring within a string?
    There are a number of ways, with varying efficiency. If you want a count
    of a certain single character (X) within a string, you can use the
    "tr///" function like so:

        $string = "ThisXlineXhasXsomeXx'sXinXit";
        $count = ($string =~ tr/X//);
        print "There are $count X characters in the string";

    This is fine if you are just looking for a single character.
...

# 3  
Old 03-28-2008
Code:
awk '$0=$0OFS NF-1' FS=, file

Use nawk or /usr/xpg4/bin/awk on Solaris.
 
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 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

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

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

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

5. UNIX for Dummies Questions & Answers

counting the occurence of a word

In a file I have to count a particular word. like i need apache how many times. I tried this $ tr "\011" "\012\012"<foo1 | tr -cd "" |sort\uniq -c but I got result like this 32 apache 18 dns 12 doctor Please sugest me (4 Replies)
Discussion started by: pranabrana
4 Replies

6. UNIX for Dummies Questions & Answers

Counting occurence of a particular character on each line

Hi, I have the following data in a flat file: abcd_efgh_ijkl_20080522.dat|20080602222508|1357 abcd_efgh_ijkl_20080522.dat|20080602222508|1357 abcd_efgh_ijkl_20080522.dat|20080602222508|1357 I need to check the no. of occurence of "|" (pipe) on each line and the output should look like below:... (4 Replies)
Discussion started by: hey_mak
4 Replies

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

8. Shell Programming and Scripting

Counting occurence of a coma

I have a .csv file that I would like to count on each line the occurences of the coma. Each line should have the same amount, but as an integrity check I would like to include in my script this count. What is the best way of doing this? (3 Replies)
Discussion started by: dbrundrett
3 Replies

9. Shell Programming and Scripting

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... (6 Replies)
Discussion started by: plelie2
6 Replies
Login or Register to Ask a Question