Change string char at index X

前端 未结 3 1247
名媛妹妹
名媛妹妹 2020-12-16 12:26

I\'m searched for a long time how to do a simple string manipulation in UNIX

I have this string:

theStr=\'...............\'
相关标签:
3条回答
  • 2020-12-16 12:56
    a="............"
    b="${a:0:4}A${a:5}"
    echo ${b}
    

    Here is one really good tutorial on string manipulation.

    0 讨论(0)
  • 2020-12-16 13:00

    I don't know if it's elegant, or which version of bash you need, but

    theStr="${theStr:0:4}A${theStr:5}"
    

    The first part returns first four characters, then the character 'A', and then all the characters starting with the 6th one

    0 讨论(0)
  • 2020-12-16 13:15

    You can achieve this with sed, the stream line editor:

    echo $theStr | sed s/./A/5

    First you pipe the output of $theStr to sed, which replaces the fifth character with A.

    0 讨论(0)
提交回复
热议问题