LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 07-19-2007, 09:29 AM   #1
powah
Member
 
Registered: Mar 2005
Distribution: FC, Gentoo
Posts: 276

Rep: Reputation: 30
characters count in a string


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";

However, I am trying to find out what to do next.

Last edited by powah; 07-19-2007 at 09:30 AM.
 
Old 07-19-2007, 10:07 AM   #2
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
Code:
echo "test" | awk 'BEGIN{ FS=""}
{
  for(i=1;i<=NF;i++){
	array[$i]++
  }
}
END { for ( i in array ) print i ": "array[i] }'
output:
Code:
 # ./test.sh
e: 1
s: 1
t: 2
 
Old 07-19-2007, 12:42 PM   #3
makyo
Member
 
Registered: Aug 2006
Location: Saint Paul, MN, USA
Distribution: {Free,Open}BSD, CentOS, Debian, Fedora, Solaris, SuSE
Posts: 735

Rep: Reputation: 76
Hi.

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
 
Old 07-19-2007, 02:03 PM   #4
bulliver
Senior Member
 
Registered: Nov 2002
Location: Edmonton AB, Canada
Distribution: Gentoo x86_64; Gentoo PPC; FreeBSD; OS X 10.9.4
Posts: 3,760
Blog Entries: 4

Rep: Reputation: 78
Quote:
$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...
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
SED - remove last four characters from string 3saul Linux - Software 12 01-16-2023 10:21 AM
command to count occurances of a string MicahCarrick Linux - Software 1 06-02-2007 10:43 PM
Count occurances of a string in a text file MicahCarrick Linux - Software 1 06-24-2006 04:25 PM
extracting first n characters of a string in perl ananthbv Programming 4 09-14-2004 07:25 AM
Handle escape characters in a string Helene Programming 7 05-01-2004 11:43 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 08:13 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration