【问题标题】:Write character/line from a string in bash? [duplicate]从bash中的字符串写入字符/行? [复制]
【发布时间】:2013-12-19 15:12:02
【问题描述】:

我想得到这个字符串 -> Example example1

以这种形式:

E  
x  
a  
m  
p  
l  
e

e  
x  
a  
m  
p  
l  
e  
1  

【问题讨论】:

    标签: string bash character line cut


    【解决方案1】:

    fold utilitywidth=1 一起使用:

    echo 'Example example1' | fold -w1
    E
    x
    a
    m
    p
    l
    e
    
    e
    x
    a
    m
    p
    l
    e
    1
    

    另一个选项是grep -o:

    echo 'Example example1' | grep -o .
    E
    x
    a
    m
    p
    l
    e
    
    e
    x
    a
    m
    p
    l
    e
    1
    

    【讨论】:

    • 这是fold的完美配方!
    • @user3061736:请考虑将其标记为“已接受”,以便以后遇到类似问题的用户可以轻松查看。
    • 不是真正的 bash 解决方案,因为您正在调用外部实用程序。
    【解决方案2】:

    使用标准的 unix 工具,您可以这样做,例如:

    echo "Example example1" | sed 's/\(.\)/\1\n/g'

    使用纯 bash:

    echo "Example example1" | while read -r -n 1 c ; do echo "$c"; done

    【讨论】:

      猜你喜欢
      • 2017-01-13
      • 2021-04-17
      • 2013-08-12
      • 1970-01-01
      • 2016-10-03
      • 2012-07-08
      • 1970-01-01
      • 1970-01-01
      • 2012-08-27
      相关资源
      最近更新 更多