The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

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 and shell scripting languages 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 06:15 AM
to copy and repeat falcondown01 Shell Programming and Scripting 4 09-07-2007 08:15 PM
Repeat Commands dereckbc UNIX for Dummies Questions & Answers 6 01-04-2005 11:15 AM
Keyboard repeat speed tecss UNIX for Advanced & Expert Users 1 05-17-2004 09:05 PM
any idea to repeat a action in VI myelvis UNIX for Dummies Questions & Answers 6 11-26-2003 06:21 AM

Reply
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 12-13-2007
ripat ripat is offline Forum Advisor  
Registered User
  
 

Join Date: Oct 2006
Location: Belgium
Posts: 438
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
  #2 (permalink)  
Old 12-13-2007
rikxik's Avatar
rikxik rikxik is offline
Registered User
  
 

Join Date: Dec 2007
Posts: 250
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
  #3 (permalink)  
Old 12-13-2007
ripat ripat is offline Forum Advisor  
Registered User
  
 

Join Date: Oct 2006
Location: Belgium
Posts: 438
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.
  #4 (permalink)  
Old 12-13-2007
radoulov's Avatar
radoulov radoulov is offline Forum Staff  
addict
  
 

Join Date: Jan 2007
Location: Варна, България / Milano, Italia
Posts: 2,860
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 07:13 AM..
  #5 (permalink)  
Old 12-13-2007
ripat ripat is offline Forum Advisor  
Registered User
  
 

Join Date: Oct 2006
Location: Belgium
Posts: 438
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.
  #6 (permalink)  
Old 12-13-2007
radoulov's Avatar
radoulov radoulov is offline Forum Staff  
addict
  
 

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

Code:
print ${(l:100::#:)}
  #7 (permalink)  
Old 10-29-2008
cfajohnson's Avatar
cfajohnson cfajohnson is offline Forum Advisor  
Shell programmer, author
  
 

Join Date: Mar 2007
Location: Toronto, Canada
Posts: 2,361
Quote:
Originally Posted by radoulov View Post
ksh93/bash:

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

That doesn't work in the ksh93 I have.

The function the OP referred to (from my book) concatenates 3 instances on each iteration.
Quote:
bash3

Code:
printf -vch  "%100s" ""
printf "%s\n" "${ch// /#}"

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.

Code:
_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
}
Reply

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 10:43 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0