Solved

Count occurrences of given character in file

Posted on 2007-03-26
9
4,897 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
  • 5
  • 3
9 Comments
 
LVL 45

Expert Comment

by:Kdo
Comment Utility
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
Comment Utility
perl -e '$c{$_}++ for/[A-Z]/g;END{ print "$_:$c{$_}\n" for sort keys %c}' < FILE
0
 
LVL 45

Expert Comment

by:Kdo
Comment Utility
Hi ozo,

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


Thanks,
Kent
0
 
LVL 84

Expert Comment

by:ozo
Comment Utility
How many A's B's and C's are there in FILE?
0
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 45

Expert Comment

by:Kdo
Comment Utility
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
Comment Utility
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 500 total points
Comment Utility
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
Comment Utility
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
Comment Utility
Thank you!
0

Featured Post

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

How to remove superseded packages in windows w60 or w61 installation media (.wim) or online system to prevent unnecessary space. w60 means Windows Vista or Windows Server 2008. w61 means Windows 7 or Windows Server 2008 R2. There are various …
Active Directory replication delay is the cause to many problems.  Here is a super easy script to force Active Directory replication to all sites with by using an elevated PowerShell command prompt, and a tool to verify your changes.
Learn how to find files with the shell using the find and locate commands. Use locate to find a needle in a haystack.: With locate, check if the file still exists.: Use find to get the actual location of the file.:
Learn how to navigate the file tree with the shell. Use pwd to print the current working directory: Use ls to list a directory's contents: Use cd to change to a new directory: Use wildcards instead of typing out long directory names: Use ../ to move…

743 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now