Search code examples
bashsubstringpad

bash 0 pad numbers on substrings


I have long lists as follows:

D6N
T69TN
K70R
M184V
T215FEG

The result must be like this:

D006N
T069TN
K070R
M184V
T215FEG

I'm new on bash, I tried approaches based in splitting it in columns and reformat. However the positions and length of 2nd and 3rd putative columns are not fixed. Thank you for any help!


Solution

  • You can do this using awk, using the built-in match function:

    awk 'match($0, /[0-9]+/) { printf "%s%03d%s\n", 
    substr($0, 0, RSTART - 1), substr($0, RSTART, RLENGTH), substr($0, RSTART + RLENGTH) }' file
    

    When match is successful, it sets two variables RSTART and RLENGTH, which can be used to extract substrings. The middle substring is formatted using %03d, to pad with leading zeros.

    Any lines not matching the pattern won't be printed.

    Another option using perl:

    perl -pe 's/\d{1,3}/sprintf("%03d", $&)/eg' file
    

    This replaces any sequence of one to three digits with a zero-padded three digit number. In this version, all lines are printed.