Need help in printf in shell script

My requirement is need to add spaces to the string with the dynamic value to printf... this is a part of shell script ..which i have , the length is not static ...

length=15
value="1234567890"
 printf "%-"$length"s\n" "$value";

The result it is printing is ... i am not sure y it is printing %-15s..

%-15s
 1234567890

You can do it like this:

 printf "%15s\n"  "$value"

For dynamic:

printf "%*s\n" $length "$value"

Guru.

1 Like

it is printing like below

*s
*s

Hey!!!

Another way is,

pat='%'${length}'s\n'
printf $pat "$value"

Cheers,
Ranga :slight_smile:

$ echo $SHELL
/bin/bash
$ cat temp.sh
length=15
value="1234567890"
printf "%*s\n" $length "$value"
$ ./temp.sh
     1234567890

This is working fine ... how can i do samething .. to append zero's instead of spaces ..

$ cat temp.sh
length=15
value="1234567890"
printf "%0*d\n" $length "$value"

$ ./temp.sh
000001234567890
1 Like

Try:

printf "%0*s\n" $length "$value"

i am getting

*s
*s
echo $SHELL
/usr/bin/ksh

Not working for me....

For zero padding in ksh, use the typeset command:

$ typeset -RZ5 x
$ x=25
$ echo $x
00025

Mapping it to your case:

$ length=15
$ value="1234567890"
$ typeset -RZ${length} value
$ echo $value
000001234567890

Guru.

1 Like

yes typeset worked thx

---------- Post updated 04-18-13 at 02:14 AM ---------- Previous update was 04-17-13 at 08:22 AM ----------

Hi guru...

typeset worked when the "value" has some value in it...
if the value sometimes empty ... then it should create the passed length zero's...
But typeset is giving spaces ...Help me on this

In case this works:

$ cat temp.sh
length=15
value="1234567890"
printf "%0*d\n" $length "$value"

value=""
printf "%0*d\n" $length "$value"

$ ./temp.sh
000001234567890
000000000000000

Mine is ksh ... it is not working

length=15
value=""
printf "%0*d\n" $length "$value"
*d
*d

Sorry about that. :mad:

If typeset does not work, one thing you could try (adjusting syntax if needed for ksh) is:

if [ -z "var" ]; then
  echo 00000000000
else
  typeset -RZ${length} value
  echo $value
fi

the length will be from runtime ... so i am not sure abt the how many zero to give

If this isn't working, you must be using an 1988 version of ksh . If you are on a Solaris/SunOS system, you could try /usr/xpg4/bin/sh or /usr/xpg6/bin/sh to get a 1993 version of ksh . You could also look for a ksh93 utility to use instead of plain ksh . Even if the ksh built-in printf doesn't recognize format specifiers like %0*d , there might be a /bin/printf or /usr/bin/printf that does understand what to do with it.

There is no options in

awk , sed ... 

You're right. Maybe this then:

if [ -z "value" ]; then
  value=0
fi
typeset -RZ${length} value
echo $value

There is also a ${value:=0} syntax you could use that sets "value" to 0 if value is not set or is null.

---------- Post updated at 02:46 AM ---------- Previous update was at 02:41 AM ----------

So in other words, you might be able to just say:

typeset -RZ${length} value
echo ${value:=0}

that sounds grt to me .. it worked...

I have another problem now

i want to find a string/number variable value length...

ex:
value=value=`echo "$i_line1" | cut -d"|" -f4` ( this will be from run time .. i mean reading this value from another file which is pipe delimiter)
value_len=`echo  "$value\c" | wc -m`

if value is empty.. it is stil giving value_len as 15...
it should be empty ...not sure what is the problem

---------- Post updated at 05:51 AM ---------- Previous update was at 04:52 AM ----------

i searched and used atlast

 
value_len=`echo  ${#value}`

Sounds like it is working now. In case not:

Why do you say "value=value=`echo" instead of just "value=`echo" expression?

It would help to have some diagnostic lines like "echo $i_line1" and "echo $value" all by themselves to help track down what is going on.