LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 08-15-2013, 10:49 PM   #1
divyashree
Senior Member
 
Registered: Apr 2007
Location: Bangalore, India
Distribution: RHEL,SuSE,CentOS,Fedora,Ubuntu
Posts: 1,386

Rep: Reputation: 135Reputation: 135
inserting variable in bash printf statement


In the following printf statement will print 1with padding 45 zeros.

Code:
printf '1%045d\n'
I want to put 45 in a variable(let n) and insert the($n) in the printf statement.

Can anyone help me to do it ?
 
Old 08-15-2013, 11:07 PM   #2
Firerat
Senior Member
 
Registered: Oct 2008
Distribution: Debian sid
Posts: 2,683

Rep: Reputation: 783Reputation: 783Reputation: 783Reputation: 783Reputation: 783Reputation: 783Reputation: 783
Not sure I understand

printf is doing what you asked,

Code:
for i in 0 {10..12};do
    printf '1%045d\n' $i
    printf '1% 45d\n' $i
    printf '1%-45dend\n' $i
    printf '1%045f\n' $i
done
1000000000000000000000000000000000000000000000
1                                            0
10                                            end
100000000000000000000000000000000000000.000000
1000000000000000000000000000000000000000000010
1                                           10
110                                           end
100000000000000000000000000000000000010.000000
1000000000000000000000000000000000000000000011
1                                           11
111                                           end
100000000000000000000000000000000000011.000000
1000000000000000000000000000000000000000000012
1                                           12
112                                           end
100000000000000000000000000000000000012.000000
if you post what you were expecting we can help you achieve that
 
Old 08-15-2013, 11:19 PM   #3
divyashree
Senior Member
 
Registered: Apr 2007
Location: Bangalore, India
Distribution: RHEL,SuSE,CentOS,Fedora,Ubuntu
Posts: 1,386

Original Poster
Rep: Reputation: 135Reputation: 135
I want to replace the 45 in with a variable. Means I want like this
Quote:
printf '1%0$VARd\n'
 
Old 08-15-2013, 11:34 PM   #4
SAbhi
Member
 
Registered: Aug 2009
Location: Bangaluru, India
Distribution: CentOS 6.5, SuSE SLED/ SLES 10.2 SP2 /11.2, Fedora 11/16
Posts: 665

Rep: Reputation: Disabled
to what i have understood it you can try something like this, though i would defer to @Firerat to comment on this further, he may have a better option.

Code:
printf 1%0${var}d
 
Old 08-15-2013, 11:37 PM   #5
Firerat
Senior Member
 
Registered: Oct 2008
Distribution: Debian sid
Posts: 2,683

Rep: Reputation: 783Reputation: 783Reputation: 783Reputation: 783Reputation: 783Reputation: 783Reputation: 783
Quote:
Originally Posted by divyashree View Post
I want to replace the 45 in with a variable. Means I want like this
Sorry, you will have to give me more context

unless,,

Something like this?
Code:
for VAR in 0 {10..12};do
   printf '1%0'$VAR'd\n' $VAR
   printf '1% '$VAR'd\n' $VAR
   printf '1%-'$VAR'dend\n' $VAR
   printf '1%0'$VAR'f\n' $VAR
done
# gives you this
10
1 0
10end
10.000000
10000000010
1        10
110        end
1010.000000
100000000011
1         11
111         end
10011.000000
1000000000012
1          12
112          end
100012.000000
 
Old 08-15-2013, 11:41 PM   #6
konsolebox
Senior Member
 
Registered: Oct 2005
Distribution: Gentoo, Slackware, LFS
Posts: 2,248
Blog Entries: 8

Rep: Reputation: 235Reputation: 235Reputation: 235
Place it also around double quotes to make it a little safer:
Code:
printf "%0${n}d\\n" 1

Last edited by konsolebox; 08-15-2013 at 11:47 PM.
 
3 members found this post helpful.
Old 08-15-2013, 11:46 PM   #7
Firerat
Senior Member
 
Registered: Oct 2008
Distribution: Debian sid
Posts: 2,683

Rep: Reputation: 783Reputation: 783Reputation: 783Reputation: 783Reputation: 783Reputation: 783Reputation: 783
Quote:
Originally Posted by konsolebox View Post
Place it also around double quotes to make it a little safer:
Code:
printf "%0${n}d\\n" 1
yeah, I need to somehow commit the expansion order to memory.
 
Old 08-15-2013, 11:50 PM   #8
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,860
Blog Entries: 1

Rep: Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869
Code:
printf '1%0*d\n' "$WIDTH" "$VALUE"

Last edited by NevemTeve; 08-16-2013 at 01:49 AM. Reason: removing 'untested'
 
Old 08-16-2013, 12:36 AM   #9
divyashree
Senior Member
 
Registered: Apr 2007
Location: Bangalore, India
Distribution: RHEL,SuSE,CentOS,Fedora,Ubuntu
Posts: 1,386

Original Poster
Rep: Reputation: 135Reputation: 135
Quote:
Originally Posted by konsolebox View Post
Place it also around double quotes to make it a little safer:
Code:
printf "%0${n}d\\n" 1
Thank you consolebox, I just simply forgot this substitution method.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Multiple string variable comparisons in an if statement in bash buee Linux - Software 4 01-02-2019 08:21 AM
[SOLVED] using printf with a variable ip_address Programming 9 03-20-2013 05:26 AM
Inserting apostrophe between a variable (bash) alexpacio Programming 3 12-09-2009 09:04 AM
Inserting command to variable in bash marhen Programming 8 04-05-2008 07:33 AM
bash variable loses contents in mysql statement thedude2010 Programming 3 06-02-2006 04:15 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 07:31 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration