On 18.05.2012 01:51, heavytull wrote:
In awk you can do it the same way as Barry showed for a printf command,
using either printf to print it or sprintf to create a string for further
use in any string contexts, as in
awk '{ padding=sprintf("%*s",4,""); x="BEGIN"padding"END"; print x }'
If you're working in shell and are using Kornshell you can define a
variable to be left/right adjusted and padded by blanks or zeroes using
the typeset declaration (with options -L, -R, or -Z). A separate padding
field can be defined as in
n=4
typeset -R $n field=""
and sample code like
printf ">>%s<< l=%d\n" "${field}" "${#field}"
field=${field// /a}
printf ">>%s<< l=%d\n" "${field}" "${#field}"
will output
>> << l=4
>>aaaa<< l=4
Kornshell has printf also as a builtin, so you won't have any process
creation penalty using that. (Whether the declarative approach or the
command call should be preferred depends on your application context.)
Janis