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
Solved

Count occurrences of given character in file

Posted on 2007-03-26
9
4,902 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: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
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

 
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

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Call Shell Script from Perl Script 6 134
Powershell Timeout - Exchange 3 74
Authenticate using sesu from script 7 57
parse a file and get data out 11 78
Introduction Regular patching is part of a system administrator's tasks. However, many patches require that the system be in single-user mode before they can be installed. A cluster patch in particular can take quite a while to apply if the machine…
Background Still having to process all these year-end "csv" files received from all these sources (including Government entities), sometimes we have the need to examine the contents due to data error, etc... As a "Unix" shop, our only readily …
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…

828 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