Shell Printf command , a little more dynamic.. | Unix Linux Forums | Shell Programming and Scripting

  Go Back    


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

Shell Printf command , a little more dynamic..

Shell Programming and Scripting


Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 02-04-2013
kumarjt kumarjt is offline
Registered User
 
Join Date: Jan 2013
Last Activity: 21 October 2013, 5:16 AM EDT
Posts: 41
Thanks: 3
Thanked 1 Time in 1 Post
Shell Printf command , a little more dynamic..

A big hello to everyone tagged to this site of knowledge . This is the first post of mine and I am looking forward to an enjoyable stint in this forum where I get to know a lot of new ideas and share whatever knowledge (its not much though ) I have acquired throughout my career so far with y'all.

To start of , I am , in a sort of stuck in a strange situation.
The requirement is to format a string to a precision , which is will be dynamic .

Lets say the string's 976461 , and we have to format it to a precision of 10 , and LEFT PAD it with zero's. So the output would be 0000976461 .

What I did is to store the precision in a Shell variable (say a ) using the following code :

Code:
a=10

and the string in another Shell variable b using :

Code:
b=976461

and use the printf command to format the string to the precision stored in variable a , and finally write the output to variable c using

Code:
c=` printf "%0'$a'd" $b`

Now , when I echo the value of c, it showing 000976,461.

Can anyone help me with this ?

Thanks
Kumarjit.

Last edited by Scrutinizer; 02-04-2013 at 04:02 AM.. Reason: code tags
Sponsored Links
    #2  
Old 02-04-2013
neutronscott's Avatar
neutronscott neutronscott is offline Forum Advisor  
script kiddie
 
Join Date: Jun 2011
Last Activity: 31 October 2014, 4:04 PM EDT
Location: McMurdo Station, Antarctica
Posts: 820
Thanks: 28
Thanked 244 Times in 230 Posts
Is this exactly how you wrote it? My shell gives an error due to the quoting. Which shell are you using? This works in dash, a mostly posix sh:


Code:
$ a=10 b=976461
$ c=$(printf "%0${a}d" "$b")
$ echo "$c"
0000976461

Sponsored Links
    #3  
Old 02-04-2013
Scrutinizer's Avatar
Scrutinizer Scrutinizer is online now Forum Staff  
Moderator
 
Join Date: Nov 2008
Last Activity: 31 October 2014, 9:51 PM EDT
Location: Amsterdam
Posts: 9,565
Thanks: 286
Thanked 2,431 Times in 2,178 Posts
Or:

Code:
c=$(printf "%0*d" "$a" "$b")

The Following User Says Thank You to Scrutinizer For This Useful Post:
neutronscott (02-04-2013)
    #4  
Old 02-04-2013
kumarjt kumarjt is offline
Registered User
 
Join Date: Jan 2013
Last Activity: 21 October 2013, 5:16 AM EDT
Posts: 41
Thanks: 3
Thanked 1 Time in 1 Post
Thanks a ton to you both ( neutronscott and Scrutinizer ), you guys got crazy skills .....

BTW, I am using Korn Shell.

To be very honest , I havent been into Shell scripting for a long period of time, but am keen to know how each of yours codes work .

@neutronscott----firstly what does ${a} in the expression "%0${a}d" stand for , and how different is it from using a blatant "%0$ad" or something like "%0'$a'd"
What I meant to ask is the significance of the {} while invoking a shell variable ?

@Scrutinizer---- I have to admit I dont even have any questions to ask BECAUSE I COULDNT MAKE THE HEADS OR TAILS OF WHAT YOU WROTE, but it works magic .
Could you please explain what your code does and the significance of "*" in the printf command ?

Thank you guys,
Kumarjit.
Sponsored Links
    #5  
Old 02-04-2013
Scrutinizer's Avatar
Scrutinizer Scrutinizer is online now Forum Staff  
Moderator
 
Join Date: Nov 2008
Last Activity: 31 October 2014, 9:51 PM EDT
Location: Amsterdam
Posts: 9,565
Thanks: 286
Thanked 2,431 Times in 2,178 Posts
Hi Kumar, from man printf:

Quote:
A field width or precision may be `*' instead of a digit string. In this case an argument supplies the field width or precision.
So


Code:
printf "%0*d" 10 "$b"

works like

Code:
printf "%010d" "$b"

The Following User Says Thank You to Scrutinizer For This Useful Post:
pamu (02-04-2013)
Sponsored Links
Closed Thread

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
help with printf command bipbip Shell Programming and Scripting 2 05-06-2012 09:23 PM
Shell script dynamic command nuthalapati Shell Programming and Scripting 2 03-10-2010 08:28 PM
passing a list of dynamic names to a "PS" command in shell script? sachin.tendulka Shell Programming and Scripting 3 11-21-2007 03:18 AM
the printf command bebop1111116 Shell Programming and Scripting 4 10-01-2006 12:40 AM
printf command FIRE Shell Programming and Scripting 2 08-07-2002 02:18 PM



All times are GMT -4. The time now is 09:51 PM.