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
So I can use this sed substitutions:
s,\([a-z ]\+\)\([0-9]\)\([a-z ]*\),\100\2\3,
s,\([a-z ]\+\)\([0-9][0-9]\)\([a-z ]*\),\10\2\3,
But, can I make one substitution, that will do the same? Something like:
if (one digit) then two leading 0
elif (two digits) then one leading 0
Regards.
I doubt that the “if – else” logic can be incorporated in one substitution command without saving the intermediate data (length of the match for instance). It doesn’t mean you can’t do it easily, though. For instance:
It uses recursion, adding one zero to all numbers that are shorter than
$Ndigits in a loop that ends when no more substitutions can be made. Therlabel basically says: try to do substitution, thengoto rif found something to substitute. See more on flow control insedhere.