Solved

Count occurrences of given character in file

Posted on 2007-03-26
9
4,901 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
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 45

Expert Comment

by:Kdo
ID: 18794867
Hi ozo,

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


Thanks,
Kent
0
Ransomware-A Revenue Bonanza for Service Providers

Ransomware – malware that gets on your customers’ computers, encrypts their data, and extorts a hefty ransom for the decryption keys – is a surging new threat.  The purpose of this eBook is to educate the reader about ransomware attacks.

 
LVL 84

Expert Comment

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

Expert Comment

by:Kdo
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 500 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

Problems using Powershell and Active Directory?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

Question has a verified solution.

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

Installing FreeBSD… FreeBSD is a darling of an operating system. The stability and usability make it a clear choice for servers and desktops (for the cunning). Savvy?  The Ports collection makes available every popular FOSS application and packag…
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 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…
In a previous video, we went over how to export a DynamoDB table into Amazon S3.  In this video, we show how to load the export from S3 into a DynamoDB table.

809 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