The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
Google UNIX.COM


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

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Repeat last entered command ? vilius Shell Programming and Scripting 5 09-25-2007 03:15 AM
to copy and repeat falcondown01 Shell Programming and Scripting 4 09-07-2007 05:15 PM
Repeat Commands dereckbc UNIX for Dummies Questions & Answers 6 01-04-2005 08:15 AM
Keyboard repeat speed tecss UNIX for Advanced & Expert Users 1 05-17-2004 06:05 PM
any idea to repeat a action in VI myelvis UNIX for Dummies Questions & Answers 6 11-26-2003 03:21 AM

Reply
 
Submit Tools LinkBack Thread Tools Search this Thread Display Modes
  #1  
Old 12-13-2007
Registered User
 

Join Date: Oct 2006
Location: Belgium
Posts: 171
repeat character with printf

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
Reply With Quote
Forum Sponsor
  #2  
Old 12-13-2007
rikxik's Avatar
Registered User
 

Join Date: Dec 2007
Posts: 105
I didn't know repeating characters was printf's speciality

If you don't mind using perl, this should be ok:

Code:
$ perl -e 'print "-" x 25,"\n"'
-------------------------
HTH
Reply With Quote
  #3  
Old 12-13-2007
Registered User
 

Join Date: Oct 2006
Location: Belgium
Posts: 171
Thanks. I was hoping to find some pure bash solution like padding with a custom character like in PHP

Code:
printf("%'#10s\n",  '');
Still looking.
Reply With Quote
  #4  
Old 12-13-2007
radoulov's Avatar
addict
 

Join Date: Jan 2007
Location: Milano, Italia/Варна, България
Posts: 1,933
ksh93/bash:

Code:
for i in {1..100};do printf "%s" "#";done;printf "\n"
zsh:

Code:
repeat 100 printf "#";print
or:

ksh93/zsh and bash:

Code:
ch="$(printf "%100s" "")"
printf "%s\n" "${ch// /#}"

bash3

Code:
printf -vch  "%100s" ""
printf "%s\n" "${ch// /#}"
and another one with zsh:

Code:
print "${$(printf "%100s" "")// /#}"

Last edited by radoulov; 12-13-2007 at 04:13 AM.
Reply With Quote
  #5  
Old 12-13-2007
Registered User
 

Join Date: Oct 2006
Location: Belgium
Posts: 171
Quote:
Originally Posted by radoulov View Post
Code:
printf -vch  "%100s" ""
printf "%s\n" "${ch// /#}"
I like that one. The best I could come up with was this:

Code:
LINE="########################################################"
echo ${LINE,0,15}
Thank you.
Reply With Quote
  #6  
Old 12-13-2007
radoulov's Avatar
addict
 

Join Date: Jan 2007
Location: Milano, Italia/Варна, България
Posts: 1,933
Sorry,
in zsh it could be just like this

Code:
print ${(l:100::#:)}
Reply With Quote
  #7  
Old 12-13-2007
Perderabo's Avatar
Unix Daemon
 

Join Date: Aug 2001
Location: Washington DC Area
Posts: 8,667
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...

Code:
$ 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.
Reply With Quote
Google The UNIX and Linux Forums
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes




All times are GMT -7. The time now is 07:44 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008. All Rights Reserved.Ad Management by RedTyger Visit The Complex Event Processing Blog

Content Relevant URLs by vBSEO 3.2.0