【问题标题】:calculate count of char use in string计算字符串中使用的字符数
【发布时间】:2015-02-26 17:53:00
【问题描述】:

我试图让这个程序工作,但我不能,我想要的脚本必须计算字符串中 char 使用的计数 例子 : 字符串=好 结果=g1o2d1 有什么方法可以编写脚本来精确计算字符串的数量,比如我的例子吗?

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

我的结果:

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

但是为了分析一些文档,我想要脚本来计算一些类似的字符 firstchar1=$(bash 脚本) …… 我需要该值才能使用另一个脚本 请给我提意见 问候

【问题讨论】:

    标签: string bash


    【解决方案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
    

    Bash: Split string into character arraycounting duplicates in a sorted sequence using command line tools

    【讨论】:

      猜你喜欢
      • 2014-10-21
      • 2018-04-20
      • 1970-01-01
      • 2019-09-14
      • 2016-10-28
      • 2018-04-20
      • 1970-01-01
      相关资源
      最近更新 更多