Count the alpha numeric


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Count the alpha numeric
# 1  
Old 04-25-2010
Count the alpha numeric

Hi all.

This is one of my interview question.

input : apple

output :

a - 1
p - 2
l - 1
e - 1

i written the code like this

Code:
echo "apple" | fold -w1 | sort | uniq -c | awk '{print $2 "-->" $1}'

is any other way to do ?

thanks
kamaraj
# 2  
Old 04-25-2010
Not shorter but with with one awk command Smilie:
Code:
echo "apple" | awk '{
  l=length
  for(i=1;i<=l;i++) {
    s[i]=c=substr($0,i,1)
    if(match(c,"[a-zA-Z]")){
      a[c]++
    }
  }
  for(i=1;i<=l;i++) {
    c=s[i]
    if(a[c]) {
      print c " - " a[c]
      delete a[c]
    }
  }
}'


Last edited by Franklin52; 04-25-2010 at 05:21 PM.. Reason: adding match command for alpahanumeric characters
# 3  
Old 04-25-2010
If this is a job interview and you'd like to dazzle your prospective employer with an impressive display of efficient time management, then you ought not to show them the following (unless you have it on good authority that they have a posix sh fetish):

Code:
#!/bin/sh

w=$1 
o=.order

while [ "$w" ]; do
    # We need to pop the first character, c, from the word, w.
    # Determine w's length and generate a glob pattern, p, of
    # appropriate length, l.  With eval's help, use parameter
    # substitution to remove all trailing characters.
    p=
    l=$((${#w}-1))
    while [ $((l--)) -gt 0 ]; do
        p=$p?
    done
    eval c=\${w%$p}

    # Discard the character we've saved in c.
    w=${w#?}

    # If it's not alphanumeric, move on to the next.
    case $c in
        [^a-zA-Z0-9]) continue;;
    esac

    # For each alphanumeric, c, create a file named $c, which
    # will store one empty newline per character instance.  Also,
    # since we need to keep track of the order of the characters,
    # check if we've seen this character before.  If not, append
    # it to the order log, o.
    [ -f $c ] || echo $c >> "$o"
    echo >> $c
done

# Exit if no alphanumerics were present.
[ -f "$o" ] || exit 0

# Tally up and print out
while read c; do
    i=0
    while read x; do
        : $((++i))
    done < $c
    echo $c - $i
done < "$o"

rm ? "$o"

Code:
$ ./alnumcounter.sh apple
a - 1
p - 2
l - 1
e - 1

$ ./alnumcounter.sh '!#$!@#@apple    *(&((*^('
a - 1
p - 2
l - 1
e - 1

If you play with this, it would be best if you did so in an empty directory Smilie

Regards,
Alister
# 4  
Old 04-25-2010
Code:
while(<DATA>){
	my %hash;
	my @arr = split("",$_);
	map {$hash{$_}++} @arr;
	foreach my $key ( keys %hash){
		print $key," -- ", $hash{$key},"\n";
	}
	print "-----------------\n";
}
__DATA__
apple
hello

# 5  
Old 04-25-2010
Code:
# echo apple | nawk '{for(i=0;++i<=NF;){if($i ~ /[a-zA-Z0-9]/){a[$i]++}}}END{for(i in a)print i,a[i]}' FS= OFS="-->"

# 6  
Old 04-27-2010
MySQL

I write a script a bit long and full bash Smilie

An example
Code:
[root@sistem1lnx ~]# cat 3
applaeaaaaatestttttechoooooooaxxxwwwyy
 
[root@sistem1lnx ~]# ./charactercount 3
a_letter_count_8
p_letter_count_2
l_letter_count_1
e_letter_count_3
t_letter_count_6
s_letter_count_1
c_letter_count_1
h_letter_count_1
o_letter_count_7
x_letter_count_3
w_letter_count_3
y_letter_count_2

The code
Code:
#!/bin/bash
#Character Count
variables () {
a=0
b=1
c=0
}
#Add New Line and Character Sorting
cat $1 | sed 's/ */\n/g' |sed '/^$/d' > tmpfile
#All Characters in others to tmp array
while read -r others
  do
    tmp[b]=$others
    ((++b))
  done < tmpfile
#Caracter Count
charcount=$b
# count decrease for "b=1"
let charcount=$charcount-1
variables ""
#Character
while read -r char
  do
    chkchar[a]=$char
    let charcountreset=$charcount+1
    charcountfor=$charcountreset
 while [ $(( charcountreset -= 1 )) -gt 0 ]
   do
     if [ "${chkchar[a]}" == "${tmp[b]}" ] ; then
        ((++count[c]))
     fi
((++b))
   done
b=1
((++a))
((++c))
  done < tmpfile
variables ""
rm -f ununiqtmpfile
for i in ${tmp[@]}
    do
     echo $i"_letter_count_"${count[c]} >> ununiqtmpfile
     ((++c))
    done
#echo "The Results"
#cat ununiqtmpfile
# Uniq Results
b=0
while read -r char
 do
  not=0
   while read -r char2
    do
      if [ "$char" == "$char2" ] ; then
         ((++count))
      fi
    done < ununiqtmpfile
  if [ $count -eq 1 ] ; then
     uniq[b]=$char
     ((++b))
  fi
 if [ $count -gt 1 ] ; then
   if [ `echo ${#uniq[@]}` -eq 0 ] ; then
      uniq[b]=$char
      ((++b))
   else
      for i in ${uniq[@]}
          do
             if [ "$i" != "$char" ] ; then
                ((++not))
                 if [ $not -eq `echo ${#uniq[@]}` ] ; then
                    uniq[b]=$char
                    ((++b))
                 fi
             fi
          done
   fi
 fi
 done < ununiqtmpfile
#Now for show the result
    for i in ${uniq[@]}
        do
          echo $i
        done

Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

How to find the count of IP addresses that belong to different subnets and display the count?

Hi, I have a file with a list of bunch of IP addresses from different VLAN's . I am trying to find the list the number of each vlan occurence in the output Here is how my file looks like 1.1.1.1 1.1.1.2 1.1.1.3 1.1.2.1 1.1.2.2 1.1.3.1 1.1.3.2 1.1.3.3 1.1.3.4 So what I am trying... (2 Replies)
Discussion started by: new2prog
2 Replies

2. Shell Programming and Scripting

Splitting the numeric vs alpha values in a column to distinct columns

How could i take an input file and split the numeric values from the alpha values (123 vs abc) to distinc columns, and if the source is blank to keep it blank (null) in both of the new columns: So if the source file had a column like: Value: |1 | |2.3| | | |No| I would... (7 Replies)
Discussion started by: driftlogic
7 Replies

3. UNIX for Dummies Questions & Answers

Script to Count the Numeric Values and get the Total

Can anyone help me in this? Here is the Secenario. I need to count the Numerical vaues from the below output Item processed: 1401 Item processed: 2839 Item processed: 1261 Item processed: 2584 Item processed: 2 Item processed: 988 Item processed: 1 Item processed: 2119 ... (3 Replies)
Discussion started by: Padmanabhan
3 Replies

4. AIX

Pattern count for numeric values in aix

Hi All , I have a small code that checks pattern of digits entered in unix mode . $ echo 201202 | wc -c 7 /* output*/ When i run same command in AIX 5.1 , i am getting output with some initial blanks $ echo 201202 | wc -c 7 ... (4 Replies)
Discussion started by: Perlbaby
4 Replies

5. Shell Programming and Scripting

Count occurences of a numeric string falling in a range

Dear all, I have numerous dat files (1.dat, 2.dat...) containing 500 numeric values each. I would like to count them, based on their range and obtain a histogram or a counter. INPUT: 1.dat 1.3 2.16 0.34 ...... 2.dat 1.54 0.94 3.13 ..... ... (3 Replies)
Discussion started by: chen.xiao.po
3 Replies

6. Shell Programming and Scripting

Sorting the alpha numeric results of Hash

Hi All I've got a perl script that I'm having a problem with when it prints the output of a hash. Some background. I'm trying to merge two file with a similar structure but with different data. Here is a portion of the script I'm using. while (<INPUT>) { my... (0 Replies)
Discussion started by: kingpin2502
0 Replies

7. UNIX for Dummies Questions & Answers

Find and Replace random numeric value with non-numeric value

Can someone tell me how to change the first column in a very large 17k line file from a random 10 digit numeric value to a non numeric value. The format of lines in the file is: 1702938475,SNU022,201004 the first 10 numbers always begin with 170 (6 Replies)
Discussion started by: Bahf1s
6 Replies

8. Shell Programming and Scripting

Perl code to differentiate numeric and non-numeric input

Hi All, Is there any code in Perl which can differentiate between numeric and non-numeric input? (11 Replies)
Discussion started by: Raynon
11 Replies

9. Shell Programming and Scripting

how to set a variable to accept alpha-numeric characters?

I am working on a shell program that needs to accept alpha-numeric input (i.e., P00375); when I use a simple 'read' statement to read in the input (i.e., read LOG), I receive the message "p00375: bad number". How must I adjust my input statement to accept alpha-numerics? Thanks! Brent (3 Replies)
Discussion started by: bcaunt
3 Replies
Login or Register to Ask a Question