Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Count occurrences of given character in file

Posted on 2007-03-26
9
Medium Priority
?
4,918 Views
Last Modified: 2013-12-26
How do I count the numbers of characters of certain types in a file "FILE"?

The output should be e.g.
A:72344
B:2374
C:3247823
etc.

etc. if FILE contains 72344 letter 'A's etc.
0
Comment
Question by:glebspy
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 3
9 Comments
 
LVL 46

Expert Comment

by:Kent Olsen
ID: 18794390
Hello glebspy,

I don't know of an easy way to do a distribution count of all characers in a file.  So let's start with the basics and work up.  :)

This will count all occurrences of the letter 'A'.

  awk '{c+=gsub(s,s)}END{print c}' s='A' {myfile}

Let's see what others can add.  (Actually, I suspect that it's pretty easy with PERL, but mine is very, very, rusty....)



Kent
0
 
LVL 84

Expert Comment

by:ozo
ID: 18794495
perl -e '$c{$_}++ for/[A-Z]/g;END{ print "$_:$c{$_}\n" for sort keys %c}' < FILE
0
 
LVL 46

Expert Comment

by:Kent Olsen
ID: 18794867
Hi ozo,

I don't get results back from your perl.  Can you check it?


Thanks,
Kent
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 84

Expert Comment

by:ozo
ID: 18794921
How many A's B's and C's are there in FILE?
0
 
LVL 46

Expert Comment

by:Kent Olsen
ID: 18794997
Hi ozo,

I ran a C source file through the perl script and get nothing.  I'm on Fedora Core 5.


  perl -e '$c{$_}++ for/[A-Z]/g;END{ print "$_:$c{$_}\n" for sort keys %c}' < t.c


Kent
0
 
LVL 84

Expert Comment

by:ozo
ID: 18795048
Sorry, that should have been
perl -ne '$c{$_}++ for/[A-Z]/g;END{ print "$_:$c{$_}\n" for sort keys %c}' < t.c
0
 
LVL 84

Accepted Solution

by:
ozo earned 2000 total points
ID: 18795159
If you want to print 0 counts
perl -ne '$c{$_}++ for/[A-Z]/g;END{ print "$_:",$c{$_}||0,"\n" for "A".."Z"}'
0
 
LVL 84

Expert Comment

by:ozo
ID: 18795439
If you want to also count 'a' as if it was 'A'
perl -lne '$c{$_}++ for uc=~/[A-Z]/g;END{ print "$_:",$c{$_}||0 for "A".."Z"}' t.c
0
 
LVL 1

Author Comment

by:glebspy
ID: 18799400
Thank you!
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This tech tip describes how to install the Solaris Operating System from a tape backup that was created using the Solaris flash archive utility. I have used this procedure on the Solaris 8 and 9 OS, and it shoudl also work well on the Solaris 10 rel…
My previous tech tip, Installing the Solaris OS From the Flash Archive On a Tape (http://www.experts-exchange.com/articles/OS/Unix/Solaris/Installing-the-Solaris-OS-From-the-Flash-Archive-on-a-Tape.html), discussed installing the Solaris Operating S…
Learn how to get help with Linux/Unix bash shell commands. Use help to read help documents for built in bash shell commands.: Use man to interface with the online reference manuals for shell commands.: Use man to search man pages for unknown command…
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
Suggested Courses

670 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question