Solved

Count occurrences of given character in file

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

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 45

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
Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

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

[Webinar] How Hackers Steal Your Credentials

Do You Know How Hackers Steal Your Credentials? Join us and Skyport Systems to learn how hackers steal your credentials and why Active Directory must be secure to stop them. Thursday, July 13, 2017 10:00 A.M. PDT

Question has a verified solution.

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

When you do backups in the Solaris Operating System, the file system must be inactive. Otherwise, the output may be inconsistent. A file system is inactive when it's unmounted or it's write-locked by the operating system. Although the fssnap utility…
Let's say you need to move the data of a file system from one partition to another. This generally involves dismounting the file system, backing it up to tapes, and restoring it to a new partition. You may also copy the file system from one place to…
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.:
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

626 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