Add leading 0 in sed substitution

后端 未结 6 806
不思量自难忘°
不思量自难忘° 2021-01-13 15:47

I have input data:

foo 24
foobar 5 bar
bar foo 125

and I\'d like to have output:

foo 024
foobar 005 bar
bar foo 125
         


        
6条回答
  •  无人及你
    2021-01-13 15:58

    Add the max number of leading zeros first, then take this number of characters from the end:

    echo 55 | sed -e 's:^:0000000:' -e 's:0\+\(.\{8\}\)$:\1:'
    
    00000055
    

提交回复
热议问题