0

I'm trying to make this program work and I can't, The script i want must should calculate count of char use in string example : string=good result=g1o2d1 is there any way to write script that calculate exactly count of string like my example or not ?

 #!/bin/bash
    string=ssstttrrrriiiinnnnngg
    z=$( for((i=0;i<${#string};i++)) do; echo ${string:i:1}; done | uniq -c )
    echo $z

my result :

s 3 t 3 r 4 i 4 n 5 g 2

but for analysis some document i want script to calculate char some like firstchar1=$( bash script ) ...... i need that value for use another script please advise me regards

timrau
  • 22,578
  • 4
  • 51
  • 64
Soheil
  • 837
  • 8
  • 17

1 Answers1

1
$ echo "abaaacdefg" | grep -o .
a
b
a
a
a
c
d
e
f
g

$ echo "abaaacdefg" | grep -o .| sort
a
a
a
a
b
c
d
e
f
g

$  echo "abaaacdefg" | grep -o .| sort |  uniq -c 
      4 a
      1 b
      1 c
      1 d
      1 e
      1 f
      1 g


$ echo "abaaacdefg" | grep -o .| sort |  uniq -c | awk '{printf $2$1}'
a4b1c1d1e1f1g1

See Bash: Split string into character array and counting duplicates in a sorted sequence using command line tools

Community
  • 1
  • 1