Counting the number of occurances of all characters (a-z) in a string


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Counting the number of occurances of all characters (a-z) in a string
# 1  
Old 07-09-2007
Counting the number of occurances of all characters (a-z) in a string

Hi,

I am trying out different scripts in PERL. I want to take a line/string as an input from the user and count the number of occurrances of all the alphabets (a..z) in the string. I tried doingit like this :

#! /opt/exp/bin/perl

print "Enter a string or line : ";
$string = <STDIN>;
chop $string;
$string =~ tr/[A-Z]/[a-z]/;
#print @string;
print "\n";
@arr = ('a' .. 'z');
foreach $val (@arr)
{
$count = ($string =~ tr/$val//);
print "$val occurred $count times\n";
}


But looks like I am going wrong somewhere. Please guide me to solve this.

Regards,
Sendhil
# 2  
Old 07-09-2007
If it can be done in shell script:
Code:
while true
do
 echo "Enter string:"
 read mInp
 mCnt=`echo $mInp | egrep -c '[a-zA-Z]'`
 if [ "$mCnt" = "1" ]; then
   echo "Found letters"
 fi
done

# 3  
Old 07-09-2007
rsendhilmani,

This had me puzzled for a while, but I think I figured out what is wrong. I found this
Quote:
Because the transliteration table is built at compile time, neither the SEARCHLIST nor
the REPLACEMENTLIST are subjected to double quote interpolation. That means that
if you want to use variables, you must use an eval():
So the tr command is trying to translate the letters "$", "v", "a", and "l", not the value that $val contains.

Try
Code:
eval "\$count = (\$string =~ tr/$val//)";

Or use the s/// instead
Code:
$count = $string =~ s/$val//g;

# 4  
Old 07-09-2007
Not trying to win any awards for maintainability... Smilie

Code:
#!/usr/bin/perl
#
# count.pl
#

use strict;
use warnings;

my $char;
my %chars=();

print "Enter a line: ";

while( $char = getc() ) {
  last if( $char =~ /\n/ );
  $chars{ $char }++;
}

my @keys = sort keys( %chars );
foreach my $key (@keys) {
  print "$key = $chars{ $key }\n";
}

Code:
$ perl count.pl
Enter a line: This is a test
  = 3
T = 1
a = 1
e = 1
h = 1
i = 2
s = 3
t = 2

# 5  
Old 07-10-2007
Hi,

Thanks a lot. It worked :-)
# 6  
Old 08-05-2008
Non-destructive counting

I know this is an older thread, but I had a slightly different scenario, where I needed non-destructive counting and thought I'd share.

The s///g and tr/// methods already mentioned remove text from the original string. With a slight modification (adding parentheses and $1), the s/// operator can be used non-destructively:

$count = $string =~ s/([a-zA-Z])/$1/g;

In my specific case, I needed to sort a list of directories by depth. That is: /, /onelevel, /two/levels, /three/level/path, etc.

My solution was the following:

@dirs = sort {($a =~s/(\/)/$1/g) <=> ($b =~s/(\/)/$1/g)} @dirs;

It sorts the list by the number of times "/" appears in the directory, which (on a Unix system) is the same as the depth. The sort function is able to obtain the counts repeatedly and still have the original strings left over.

Note that my code doesn't correctly handle doubled slashes (e.g. "/my//dir") or relative paths (e.g., "../dir" or "/my/dir/../dir2"), but it's easy to handle those by ensuring that the paths are all absolute prior to the above line of code.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Counting number of single quotes in a string

i need to be able to count the number of single quotes ' in the entire string below: "description":"DevOps- Test VM's, System Admins Test VM's ", awk can most likely do this, but here's my attempt using egrep: echo "${STRING}" | egrep -wc '"'"\'"'"' or echo "${STRING}" | egrep -wc... (11 Replies)
Discussion started by: SkySmart
11 Replies

2. Shell Programming and Scripting

Counting the number of characters

Hi all, Can someone help me in getting the following o/p I/p:... (7 Replies)
Discussion started by: Sri3001
7 Replies

3. Shell Programming and Scripting

counting the number of characters in the filename of all files in a directory?

I am trying to display the output of ls and also print the number of characters in EVERY file name. This is what I have so far: #!/bin/sh for x in `ls`; do echo The number of characters in x | wc -m done Any help appreciated (1 Reply)
Discussion started by: LinuxNubBrah
1 Replies

4. Shell Programming and Scripting

Counting number of records with string row delimiter

HI, i have a file like this t.txt f1|_f2|_ f1|_f2|_ f1|_f2|_ as if col delimiter is |_ and row delimiter |_\n trying to count number of records using awk $ awk 'BEGIN{FS="|_" ; RS="~~\n"} {n++}END{print n} ' t.txt 7 wondering how can i count this to 3 ? thx (9 Replies)
Discussion started by: aksforum
9 Replies

5. UNIX for Dummies Questions & Answers

counting occurrence of characters in a string

Hello, I have a string like this 0:1:2:0:2:2:4:0:0:0:-200:500...... what i want is to break down how many different characters are there and their count. For example for above string it should display 0 - 5 times 1 - 1 times 2 - 3 times 4 - 1 times . . . I am stuck in writing... (8 Replies)
Discussion started by: exit86
8 Replies

6. UNIX for Dummies Questions & Answers

AWK - number of specified characters in a string

Hello, I'm new to using AWK and would be grateful for some basic advice to get me started. I have a file consisting of 10 fields. Initially I wish to calculate the number of . , ~ and ^ characters in the 9th field ($9) of each line. This particular string also contains alphabetical... (6 Replies)
Discussion started by: Olly
6 Replies

7. Shell Programming and Scripting

How to count number of occurances of string in a file?

Gurus, Need little guidance. I have A.txt and B.txt file. B.txt file contains Unique strings. Sample content of B.txt file for which i cut the fourth column uniquely and output directed to B.txt file And A.txt file contains the above string as a fourth column which is last column. So A.txt... (7 Replies)
Discussion started by: Shirisha
7 Replies

8. Shell Programming and Scripting

number of characters in a string

Hi there, I have some user input in a variable called $VAR, and i need to ensure that the string is 5 or less characters .... does anybody know how i can count the characters in the variables ? any help would be great, cheers (2 Replies)
Discussion started by: rethink
2 Replies

9. UNIX for Dummies Questions & Answers

count the number of files which have a search string, but counting the file only once

I need to count the number of files which have a search string, but counting the file only once if search string is found. eg: File1: Please note that there are 2 occurances of "aaa" aaa bbb ccc aaa File2: Please note that there are 3 occurances of "aaa" aaa bbb ccc... (1 Reply)
Discussion started by: sudheshnaiyer
1 Replies

10. UNIX for Dummies Questions & Answers

Counting Occurances in Two Files

I have two files I want to compare, one is a list of variables and the other is a text file COBOL program. Basically what I want to do is display only those variables that appear in the COBOL program only once. However I would also accept a count of each variable as it appears in the COBOL... (2 Replies)
Discussion started by: Keith Gergel
2 Replies
Login or Register to Ask a Question