LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   inserting variable in bash printf statement (https://www.linuxquestions.org/questions/programming-9/inserting-variable-in-bash-printf-statement-4175473561/)

divyashree 08-15-2013 10:49 PM

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 ?

Firerat 08-15-2013 11:07 PM

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

divyashree 08-15-2013 11:19 PM

I want to replace the 45 in with a variable. Means I want like this
Quote:

printf '1%0$VARd\n'

SAbhi 08-15-2013 11:34 PM

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

Firerat 08-15-2013 11:37 PM

Quote:

Originally Posted by divyashree (Post 5010152)
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


konsolebox 08-15-2013 11:41 PM

Place it also around double quotes to make it a little safer:
Code:

printf "%0${n}d\\n" 1

Firerat 08-15-2013 11:46 PM

Quote:

Originally Posted by konsolebox (Post 5010161)
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.

NevemTeve 08-15-2013 11:50 PM

Code:

printf '1%0*d\n' "$WIDTH" "$VALUE"

divyashree 08-16-2013 12:36 AM

Quote:

Originally Posted by konsolebox (Post 5010161)
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.


All times are GMT -5. The time now is 12:23 PM.