|
|
|
|
||||||
| comp.unix.shell Using and programming the Unix shell. |
![]() |
|
|
LinkBack | Outils de la discussion |
|
|
#1 |
|
Messages: n/a
Hébergeur: |
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 |
|
|
|
#2 |
|
Messages: n/a
Hébergeur: |
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. |
|
|
|
#3 |
|
Messages: n/a
Hébergeur: |
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 |
|
|
|
#4 |
|
Messages: n/a
Hébergeur: |
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 |
|
|
|
#5 |
|
Messages: n/a
Hébergeur: |
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 |
|
|
|
#6 |
|
Messages: n/a
Hébergeur: |
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 > > > |
|
|
|
#7 |
|
Messages: n/a
Hébergeur: |
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 |
|
|
|
#8 |
|
Messages: n/a
Hébergeur: |
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 |
|
![]() |
| Outils de la discussion | |
|
|