repeat character with printf


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting repeat character with printf
# 1  
Old 12-13-2007
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  
Old 12-13-2007
I didn't know repeating characters was printf's speciality Smilie

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

Code:
$ perl -e 'print "-" x 25,"\n"'
-------------------------

HTH
# 3  
Old 12-13-2007
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  
Old 12-13-2007
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 Smilie

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  
Old 12-13-2007
Quote:
Originally Posted by radoulov
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  
Old 12-13-2007
Sorry,
in zsh it could be just like this Smilie

Code:
print ${(l:100::#:)}

# 7  
Old 12-13-2007
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.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to repeat a character in a field if it's a single character?

I have a csv dataset like this : C,rs18768 G,rs13785 GA,rs1065 G,rs1801279 T,rs9274407 A,rs730012 I'm thinking of use like awk, sed to covert the dataset to this format: (if it's two character, then keep the same) CC,rs18768 GG,rs13785 GA,rs1065 GG,rs1801279 TT,rs9274407... (7 Replies)
Discussion started by: nengcheng
7 Replies

2. Shell Programming and Scripting

awk sed to repeat every character on same position from the upper line replacing whitespace

Hello is it possible with awk or sed to replace any white space with the previous line characters in the same position? I am asking this because the file I have doesn't always follow a pattern. For example the file I have is the result of a command to obtain windows ACLs: icacls C:\ /t... (5 Replies)
Discussion started by: nakaedu
5 Replies

3. Shell Programming and Scripting

Repeat for different variable

Hey, I've created following script: var1=test1 setA=testA if ... touch $setA/$var1 ... fi I would like now the repeat the command touch (in this example) for different variables. So below, the varX should run 3 times (var1, var2, var4). Var3 is skipped in this example... (4 Replies)
Discussion started by: brononius
4 Replies

4. Shell Programming and Scripting

Repeat a command for one sec

How to repeat the execution of a simple command like the following for 1 sec ? echo Hi The completion time for the command is not known, but we need to calculate the number of times this commans executes successfully within 1 sec. Thanks Kumarjit (5 Replies)
Discussion started by: kumarjt
5 Replies

5. Shell Programming and Scripting

Ping and repeat ?

How do i write a loop ping to see if it get timeout or hang ? it should loop every 30 second to ping a server ? ping -c 5 -t 15 www.google.com if ]; then date '+%Y-%m-%d %H:%M:%S Connection Unavailable' >> /home/sabercats/checkconnection.log else date '+%Y-%m-%d %H:%M:%S Connection... (3 Replies)
Discussion started by: sabercats
3 Replies

6. Shell Programming and Scripting

Repeat using for loop

I have a file like this 2011-10-10 10:46:00,1-1-13-1-1,151510,ALCLA0A84D2C 2011-10-10 10:46:00,1-1-13-1-1,151520,65537 2011-10-10 10:46:00,1-1-13-1-1,151515,46932 2011-10-10 10:46:00,1-1-13-1-1,151521,32769 2011-10-10 10:46:00,1-1-13-1-1,151522,32769 2011-10-10... (4 Replies)
Discussion started by: LavanyaP
4 Replies

7. UNIX for Dummies Questions & Answers

SED: Can't Repeat Search Character in SED Output

I'm not sure if the problem I'm seeing is an artifact of sed or simply a beginner's mistake. Here's the problem: I want to add a zero-width space following each underscore between XML tags. For example, if I had the following xml: <MY_BIG_TAG>This_is_a_test</MY_BIG_TAG> It should look like... (8 Replies)
Discussion started by: rhetoric101
8 Replies

8. Shell Programming and Scripting

printf with Character String

I am trying to use printf with a character string that is used within a do loop. The problem is that while in the loop, the printf prints the variable name instead of the value. The do loop calls the variable name from a text file (called device.txt): while read device do cat $device.clean... (2 Replies)
Discussion started by: dleblanc67
2 Replies

9. Shell Programming and Scripting

to copy and repeat

Hi All, I have done some looking at other threads but haven't found quite what I am looking for. I am a newbie to scripting and haven't got to where I want to you but here is my basic question. I have a script to copy a file and send it to another file with a date and time stamp. What I want to... (4 Replies)
Discussion started by: falcondown01
4 Replies

10. UNIX for Dummies Questions & Answers

Repeat Commands

On my system I use Escape "k" to go back in commands. I read on tutorials that it is ctrl p, but that does not work on my system. Anyone know what the command to go foward is? (6 Replies)
Discussion started by: dereckbc
6 Replies
Login or Register to Ask a Question