Padding printf for Strings by 0

后端 未结 3 1098
心在旅途
心在旅途 2021-01-12 21:33

Is there a way to replace the space character to 0 in printf padding for field width

Code used

printf(\"%010s\",\"this\");

Doesnt s

3条回答
  •  爱一瞬间的悲伤
    2021-01-12 22:16

    what about

     test="ABCD"
     printf "%0$(expr 9 - ${#test})d%s" 0 $test
    

    that will give you what you need too.

     ~:00000ABCD
    

    or is you want to padd with other numbers just change

      printf "%0$(expr 9 - ${#test})d%s" 1 $test
    

    will give you

     ~:11111ABCD
    

提交回复
热议问题