Append 0's based on length

I'm having data like this,

"8955719","186497034","0001","M","3"
"8955719","186497034","0002","M","10"
"8955719","186497034","0003","M","10"
"8955719","186497034","0004","M","3"
"8955723","186499034","0001","M","3"
"8955723","186499034","0002","M","10"
"8955723","186499034","0003","M","10"
"8955723","186499034","0004","M","3"

My requirement is to change last column values to 4 digits based on value length. if value is single digit then we need to append three 0's in front of the value. if 2 two digit then two zeros need to append two 0's.

expected output :

"8955719","186497034","0001","M","0003"
"8955719","186497034","0002","M","0010"
"8955719","186497034","0003","M","0010"
"8955719","186497034","0004","M","0003"
"8955723","186499034","0001","M","0003"
"8955723","186499034","0002","M","0010"
"8955723","186499034","0003","M","0010"
"8955723","186499034","0004","M","0003"

Please help me.

Thanks in advance.

Question: what if the last column has a number with 5 or more digits? Would you want to retain the number or the length?

The code below retains the number in that case.

$ 
$ cat data_1.txt
"8955719","186497034","0001","M","3"
"8955719","186497034","0002","M","10"
"8955719","186497034","0003","M","10"
"8955719","186497034","0004","M","3"
"8955723","186499034","0001","M","3"
"8955723","186499034","0002","M","10"
"8955723","186499034","0003","M","10"
"8955723","186499034","0004","M","3"
"8955723","186499034","0004","M","1234"
"8955723","186499034","0004","M","12345"
"8955723","186499034","0004","M","123456"
"8955723","186499034","0004","M","1234567"
$ 
$ perl -F, -lane 'BEGIN{$,=","} $F[4] =~ s/(")(\d+)(")/$1.sprintf("%04d",$2).$3/eg; print @F' data_1.txt
"8955719","186497034","0001","M","0003"
"8955719","186497034","0002","M","0010"
"8955719","186497034","0003","M","0010"
"8955719","186497034","0004","M","0003"
"8955723","186499034","0001","M","0003"
"8955723","186499034","0002","M","0010"
"8955723","186499034","0003","M","0010"
"8955723","186499034","0004","M","0003"
"8955723","186499034","0004","M","1234"
"8955723","186499034","0004","M","12345"
"8955723","186499034","0004","M","123456"
"8955723","186499034","0004","M","1234567"
$
$ 

Hi durden_tyler,

Thank you, its working. :b: :slight_smile:

as of now last column length will not exceed 4 char.

Try:

awk '{split($NF,F,/"/); $NF=sprintf("\"%04d\"",F[2])}1' FS=, OFS=, file

--
or, using double quotes as the field separator:

awk '{$(NF-1)=sprintf("%04d", $(NF-1))}1' FS=\" OFS=\" file