Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

how to append $var zeros to a string 3

Status
Not open for further replies.

entrylevel

Technical User
Nov 29, 2001
46
CA
Hi all,

is there a simple way in /bin/sh to append certain amount zeros at the begining of a string or at the end of a string.

say I want to add $var=5 zeros at the begining of the string "teststring", make it like "00000teststring"

and append $var=3 zeros at the end of the string "teststring", make it as "teststring000"

thanks and regards!


 
[tt]
awk -v n=$var '
BEGIN{for(;i<n;i++)printf "0"
print "teststring"}'
[/tt]
 
beg=5;end=3;test=teststring
test=`printf "%0"$beg"d" 0`$test`printf "%0"$end"d" 0`
echo $test

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ222-2244
 
Hi thanks! that helps, by the way, is there a function to identify the length of the string, say...

the length of the string "teststring" is 10 (characters)? Thanks.

Regards!
 
expr length "$test"

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ222-2244
 
Hi,

How come I tried it on Linux/AIX/HP-UX with

expr length "string"

with no problem, but for solaris ... it says

# expr length "string"
expr: syntax error

# expr 8 + 6
14

thanks and regards!
 
From man expr
Code:
  /usr/bin/expr
     Example 2: Return the number of bytes in $VAR:
 
     example$ expr "$VAR" : '.*'

Columb Healy
 
given that you want to prepend zeros to a string and know how long a string is it looks like what you realy want to do is pad numeric variables with leading zeros to get a fixed length. If this is the case then, on Solaris ksh at least, try
Code:
printf "%10.10d" $myvar


Columb Healy
 
hi Columb,

thanks, yes that's what I wanted to do, can you explain a little what is %10 stand for? i tried with

printf "%8.10d" $myvar

got the same output with "%10.10d", want to learn the diff.

Regards!
 
And this ?
printf "%08d" $myvar

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ222-2244
 
hi PH,

yeah, that works too. what is that %0 ? thanks.


Regards!
 
man printf

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ222-2244
 
A full explanation is given in
Code:
man format 5
The digit before the . is field width. For the %d format this is left padded with 0's as required. The digit after the . is precision and mainly relates to the %f or %g formats. I gave you both because I couldn't remember off the top of my head which was which. PHV's shorter simpler constuct will do just as well.

Columb Healy
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top