PHWinfo banniere

Titres
PORTAIL ANNUAIRE ARTICLES COMPARATEUR HÉBERGEURS DEVIS FORUMS RÉDUCTEUR D'URL
Précédent   PHWinfo > Forums Hébergement > Forum Serveur - Sécurité et techniques > comp.unix.shell > Bash: fast way to repeat string???
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
comp.unix.shell Using and programming the Unix shell.

Bash: fast way to repeat string???

Réponse
 
LinkBack Outils de la discussion
Vieux 31/12/2007, 07h48   #1
lihao0129@gmail.com
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Bash: fast way to repeat string???

Is there any bash commands to repeat a string, i.e. I want to output
60 '#' (just an example, need to repeat an arbitrary string anyway) in
a single line, say

echo " ##################...[cut]...##"

which is awkward, is there any fast way to achieve this??

many thanks,
lihao
  Réponse avec citation
Vieux 31/12/2007, 08h03   #2
Icarus Sparry
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Bash: fast way to repeat string???

On Sun, 30 Dec 2007 23:48:44 -0800, lihao0129@gmail.com wrote:

> Is there any bash commands to repeat a string, i.e. I want to output 60
> '#' (just an example, need to repeat an arbitrary string anyway) in a
> single line, say
>
> echo " ##################...[cut]...##"
>
> which is awkward, is there any fast way to achieve this??
>
> many thanks,
> lihao


If you are in emacs mode, then typing the following sequence of characters

e c h o space " space esc 6 esc 0 # "

will do what you ask for for a single character. You can set up a macro
that inserts a string and repeat that for the arbitrary string case.

Control-X ( a b c Control-X ) esc 1 esc 9 control-X control-E

to insert 20 copies of "abc".

Or you could write a loop.
Or you use your favorite editor and its facilities to repeat things and
write the command in a script file.
  Réponse avec citation
Vieux 31/12/2007, 08h53   #3
Chris F.A. Johnson
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Bash: fast way to repeat string???

On 2007-12-31, lihao0129@gmail.com wrote:
>
> Is there any bash commands to repeat a string, i.e. I want to output
> 60 '#' (just an example, need to repeat an arbitrary string anyway) in
> a single line, say
>
> echo " ##################...[cut]...##"
>
> which is awkward, is there any fast way to achieve this??


printf -v rept "%60s" ' ' # Older versions: rept=$( printf "%60s" ' ' )
rept=${rept//?/#}


Or:

rept=#
while [ ${#rept} -lt 60 ]
do
rept=$rept$rept$rept
done
repr=${rept:0:60}


Or, for any POSIX shell:

rept=#
while [ ${#rept} -lt 60 ]
do
rept=$rept$rept$rept
done
while [ ${#rept} -gt 60 ]
do
rept=${rept#?}
done


Or:

printf "%60s\n" " " | tr ' ' '#'


Or.....

--
Chris F.A. Johnson, author <http://cfaj.freeshell.org/shell/>
Shell Scripting Recipes: A Problem-Solution Approach (2005, Apress)
===== My code in this post, if any, assumes the POSIX locale
===== and is released under the GNU General Public Licence
  Réponse avec citation
Vieux 31/12/2007, 19h13   #4
Janis Papanagnou
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Bash: fast way to repeat string???

Icarus Sparry wrote:
> On Sun, 30 Dec 2007 23:48:44 -0800, lihao0129@gmail.com wrote:
>
>
>>Is there any bash commands to repeat a string, i.e. I want to output 60
>>'#' (just an example, need to repeat an arbitrary string anyway) in a
>>single line, say
>>
>> echo " ##################...[cut]...##"
>>
>>which is awkward, is there any fast way to achieve this??
>>
>>many thanks,
>>lihao

>
>
> If you are in emacs mode, then typing the following sequence of characters
>
> e c h o space " space esc 6 esc 0 # "


Hmm.. - in vi/vim and kornshell's vi mode it would be...

e c h o space " space " esc 6 0 i # esc

....but bash seems not to support that. (Just wondering.)

>
> will do what you ask for for a single character. You can set up a macro
> that inserts a string and repeat that for the arbitrary string case.
>
> Control-X ( a b c Control-X ) esc 1 esc 9 control-X control-E
>
> to insert 20 copies of "abc".
>
> Or you could write a loop.
> Or you use your favorite editor and its facilities to repeat things and
> write the command in a script file.


I suppose the OP wants some terse script expression like echo "#"{60}
(similar to perl's 'x 60') which doesn't seem to be supported in bash.

So I'd resort to s=$( printf "%60s" ); echo " ${s// /#}"

Janis
  Réponse avec citation
Vieux 31/12/2007, 19h31   #5
lihao0129@gmail.com
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Bash: fast way to repeat string???

Hi, thanks both for your hints:- )

On Dec 31, 3:53am, "Chris F.A. Johnson" <cfajohn...@gmail.com> wrote:
> On 2007-12-31, lihao0...@gmail.com wrote:
>
> > Is there any bash commands to repeat a string, i.e. I want to output
> > 60 '#' (just an example, need to repeat an arbitrary string anyway) in
> > a single line, say

>
> > echo " ##################...[cut]...##"

>
> > which is awkward, is there any fast way to achieve this??

>
> printf -v rept "%60s" ' ' # Older versions: rept=$( printf "%60s" ' ' )
> rept=${rept//?/#}
>
> Or:


Very nice, I finally came up with a solution like:

A=$(seq 60)
B=${A//??/#}

works pretty well. :-)

lihao

> rept=#
> while [ ${#rept} -lt 60 ]
> do
> rept=$rept$rept$rept
> done
> repr=${rept:0:60}
>
> Or, for any POSIX shell:
>
> rept=#
> while [ ${#rept} -lt 60 ]
> do
> rept=$rept$rept$rept
> done
> while [ ${#rept} -gt 60 ]
> do
> rept=${rept#?}
> done
>
> Or:
>
> printf "%60s\n" " " | tr ' ' '#'
>
> Or.....
>
> --
> Chris F.A. Johnson, author <http://cfaj.freeshell.org/shell/>
> Shell Scripting Recipes: A Problem-Solution Approach (2005, Apress)
> ===== My code in this post, if any, assumes the POSIX locale
> ===== and is released under the GNU General Public Licence


  Réponse avec citation
Vieux 31/12/2007, 19h37   #6
Janis Papanagnou
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Bash: fast way to repeat string???

lihao0129@gmail.com wrote:
> Hi, thanks both for your hints:- )
>
> On Dec 31, 3:53 am, "Chris F.A. Johnson" <cfajohn...@gmail.com> wrote:
>
>>On 2007-12-31, lihao0...@gmail.com wrote:
>>
>>
>>>Is there any bash commands to repeat a string, i.e. I want to output
>>>60 '#' (just an example, need to repeat an arbitrary string anyway) in
>>>a single line, say

>>
>>> echo " ##################...[cut]...##"

>>
>>>which is awkward, is there any fast way to achieve this??

>>
>>printf -v rept "%60s" ' ' # Older versions: rept=$( printf "%60s" ' ' )
>>rept=${rept//?/#}
>>
>> Or:

>
>
> Very nice, I finally came up with a solution like:
>
> A=$(seq 60)
> B=${A//??/#}
>
> works pretty well. :-)


But that will not produce a sequence of 60 #'es.
Compare the two outputs...

$ A=$(seq 60) ; B=${A//??/#} ; echo ${#B}
85

$ A=$(printf "%60s") ; B=${A// /#} ; echo ${#B}
60

And your code will produce wrong output depending on the
length of the sequence; try 'seq 61' to see what I mean.

Janis

>
> lihao
>
>
>>rept=#
>>while [ ${#rept} -lt 60 ]
>>do
>> rept=$rept$rept$rept
>>done
>>repr=${rept:0:60}
>>
>> Or, for any POSIX shell:
>>
>>rept=#
>>while [ ${#rept} -lt 60 ]
>>do
>> rept=$rept$rept$rept
>>done
>>while [ ${#rept} -gt 60 ]
>>do
>> rept=${rept#?}
>>done
>>
>> Or:
>>
>>printf "%60s\n" " " | tr ' ' '#'
>>
>> Or.....
>>
>>--
>> Chris F.A. Johnson, author <http://cfaj.freeshell.org/shell/>
>> Shell Scripting Recipes: A Problem-Solution Approach (2005, Apress)
>> ===== My code in this post, if any, assumes the POSIX locale
>> ===== and is released under the GNU General Public Licence

>
>
>

  Réponse avec citation
Vieux 31/12/2007, 19h38   #7
lihao0129@gmail.com
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Bash: fast way to repeat string???

On Dec 31, 2:13pm, Janis Papanagnou <Janis_Papanag...@hotmail.com>
wrote:
> Icarus Sparry wrote:
> > On Sun, 30 Dec 2007 23:48:44 -0800, lihao0...@gmail.com wrote:

>
> >>Is there any bash commands to repeat a string, i.e. I want to output 60
> >>'#' (just an example, need to repeat an arbitrary string anyway) in a
> >>single line, say

>
> >> echo " ##################...[cut]...##"

>
> >>which is awkward, is there any fast way to achieve this??

>
> >>many thanks,
> >>lihao

>
> > If you are in emacs mode, then typing the following sequence of characters

>
> > e c h o space " space esc 6 esc 0 # "

>
> Hmm.. - in vi/vim and kornshell's vi mode it would be...
>
> e c h o space " space " esc 6 0 i # esc
>
> ...but bash seems not to support that. (Just wondering.)
>
>
>
> > will do what you ask for for a single character. You can set up a macro
> > that inserts a string and repeat that for the arbitrary string case.

>
> > Control-X ( a b c Control-X ) esc 1 esc 9 control-X control-E

>
> > to insert 20 copies of "abc".

>
> > Or you could write a loop.
> > Or you use your favorite editor and its facilities to repeat things and
> > write the command in a script file.

>
> I suppose the OP wants some terse script expression like echo "#"{60}
> (similar to perl's 'x 60') which doesn't seem to be supported in bash.
>
> So I'd resort to s=$( printf "%60s" ); echo " ${s// /#}"
>


Thanks, you are right, and I want this to show up in my bash
script :- )

"printf" is much better than "seq" I used. :-)

lihao
  Réponse avec citation
Vieux 31/12/2007, 19h41   #8
lihao0129@gmail.com
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Bash: fast way to repeat string???

On Dec 31, 2:37pm, Janis Papanagnou <Janis_Papanag...@hotmail.com>
wrote:
> lihao0...@gmail.com wrote:
> > Hi, thanks both for your hints:- )

>
> > On Dec 31, 3:53 am, "Chris F.A. Johnson" <cfajohn...@gmail.com> wrote:

>
> >>On 2007-12-31, lihao0...@gmail.com wrote:

>
> >>>Is there any bash commands to repeat a string, i.e. I want to output
> >>>60 '#' (just an example, need to repeat an arbitrary string anyway) in
> >>>a single line, say

>
> >>> echo " ##################...[cut]...##"

>
> >>>which is awkward, is there any fast way to achieve this??

>
> >>printf -v rept "%60s" ' ' # Older versions: rept=$( printf "%60s" ' ' )
> >>rept=${rept//?/#}

>
> >> Or:

>
> > Very nice, I finally came up with a solution like:

>
> > A=$(seq 60)
> > B=${A//??/#}

>
> > works pretty well. :-)

>
> But that will not produce a sequence of 60 #'es.
> Compare the two outputs...
>
> $ A=$(seq 60) ; B=${A//??/#} ; echo ${#B}
> 85
>
> $ A=$(printf "%60s") ; B=${A// /#} ; echo ${#B}
> 60
>
> And your code will produce wrong output depending on the
> length of the sequence; try 'seq 61' to see what I mean.
>
> Janis
>


ah, you are right. :-)

lihao

  Réponse avec citation
Réponse


Outils de la discussion

Règles de messages
Vous ne pouvez pas créer de nouvelles discussions
Vous ne pouvez pas envoyer des réponses
Vous ne pouvez pas envoyer des pièces jointes
Vous ne pouvez pas modifier vos messages

Les balises BB sont activées : oui
Les smileys sont activés : oui
La balise [IMG] est activée : oui
Le code HTML peut être employé : non
Trackbacks are oui
Pingbacks are oui
Refbacks are oui


Fuseau horaire GMT +1. Il est actuellement 12h26.


Édité par : vBulletin® version 3.7.2
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC5 Tous droits réservés.
Version française #16 par l'association vBulletin francophone
PHWinfo est un site Éducation Sans Frontières
Ad Management by RedTyger
©Tous droits réservés par les parties respectives
Page generated in 0,15187 seconds with 16 queries