It's all in the subject. I try to figure out how to repeat a character a number of time with printf.
For example to draw a line in a script output.
Thks
It's all in the subject. I try to figure out how to repeat a character a number of time with printf.
For example to draw a line in a script output.
Thks
I didn't know repeating characters was printf's speciality
If you don't mind using perl, this should be ok:
$ perl -e 'print "-" x 25,"\n"'
-------------------------
HTH
Thanks. I was hoping to find some pure bash solution like padding with a custom character like in PHP
printf("%'#10s\n", '');
Still looking.
ksh93/bash:
for i in {1..100};do printf "%s" "#";done;printf "\n"
zsh:
repeat 100 printf "#";print
or:
ksh93/zsh and bash:
ch="$(printf "%100s" "")"
printf "%s\n" "${ch// /#}"
bash3
printf -vch "%100s" ""
printf "%s\n" "${ch// /#}"
and another one with zsh:
print "${$(printf "%100s" "")// /#}"
I like that one. The best I could come up with was this:
LINE="########################################################"
echo ${LINE,0,15}
Thank you.
Sorry,
in zsh it could be just like this
print ${(l:100::#:)}
I have never found a real great ksh method that doesn't resort to the extentions of ksh93. But if I need this, I use a function...
$ function fill { typeset i=$1 c="$2" s="" ; while ((i)) ; do ((i=i-1)) ; s="$s$c" ; done ; echo "$s" ; }
$ s=$(fill 15 \*)
$ echo "$s"
***************
$
In retrospect, "repeat" might have been a better name for the function than "fill". Maybe I'll change the name next time I use it.
here's the awk way- 65 '-':
nawk 'BEGIN{$65=OFS="-";print}'
ruby
ruby -e 'puts"-"*65'
That is tight.
That doesn't work in the ksh93 I have.
The function the OP referred to (from my book) concatenates 3 instances on each iteration.
That's a nice idea.
I used it (somewhat modified) in a couple of functions so that the character and number of repetitions can be easily specified:
The first function stores the result in a variable, by default $_REPEAT.
The second prints the result.
_repeat() ## USAGE: _repeat STRING NUM [VAR]
{
eval "printf -v ${3:-_REPEAT} '$1%.0s' {1..$2}"
}
repeat() ## USAGE: repeat STRING NUM
{
eval "printf '$1%.0s' {1..$2} "
echo
}
An old thread I know but could be useful to some...
For ksh:
eval "$(resize)"
yes \- | head -${COLUMNS:-80} | paste -d \\0 -s -
Tim
printf "%080d"|tr "0" "-"