ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
How to count all the characters in a string?
e.g.
input string is "test".
Output will be like this:
e : 1
s : 1
t : 2
P.S.
I know how to count a certain character (X) within a string with perl by using the tr/// function like so:
$string="ThisXlineXhasXsomeXx'sXinXit":
$count = ($string =~ tr/X//);
print "There are $count Xs in the string";
With shell commands, will handle multiple arguments, files as well as strings:
Code:
#!/bin/sh
# @(#) s1 Demonstrate splitting strings, counting characters.
set -o nounset
echo
echo "GNU bash $BASH_VERSION" >&2
sed --version | head -1 >&2
sort --version | head -1 >&2
echo
F=${1?" $0: must supply an argument"}
j=0
for F in $*
do
(( j++ ))
if [ ! -f "$F" ]
then
echo " note - :$F: is not a file, treated as string."
command="echo"
else
command="cat"
fi
echo
echo " Contents of item $j is:"
$command $F |
nl
echo
echo " Character counts are:"
$command $F |
sed -e 's/\(.\)/\1\n/g' |
sort |
uniq -c
done
exit 0
producing:
Code:
% ./s1 test data1
GNU bash 2.05b.0(1)-release
GNU sed version 4.1.2
sort (coreutils) 5.2.1
note - :test: is not a file, treated as string.
Contents of item 1 is:
1 test
Character counts are:
1
1 e
1 s
2 t
Contents of item 2 is:
1 Now is the time
2 for all good men
3 to come to the aid
4 of their country.
Character counts are:
4
12
1 .
1 N
2 a
2 c
2 d
6 e
2 f
1 g
3 h
4 i
2 l
3 m
2 n
9 o
3 r
1 s
7 t
1 u
1 w
1 y
If you use this on longish files, you could adapt the awk script from ghostdog74 in place of the group of sed - sort - uniq commands. See man pages for details ... cheers, makyo
$string="ThisXlineXhasXsomeXx'sXinXit":
$count = ($string =~ tr/X//);
print "There are $count Xs in the string";
However, I am trying to find out what to do next.
Code:
# psuedocode
lettercount = Hash
for char in string
if char in lettercount
lettercount[char]++
else
lettercount[char] = 1
Basically, create a hash to hold the lettercounts (key is the char, value is the count). Loop over the string. If the character is in the hash, add 1 to its value. If it is not in the hash, add it with a value of 1...
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.