From: Ed Morton on 17 Jun 2010 10:02 On 6/17/2010 8:53 AM, John Kelly wrote: > On Thu, 17 Jun 2010 07:59:31 -0500, Ed Morton<mortonspam(a)gmail.com> > wrote: > >>> $ printf "%020s\n" | tr '0' '=' >>> ==================== > >> ...nor do I know why I thought "0" was a better char for replacement than " ": >> >> $ printf "%20s\n" | tr ' ' '=' >> ==================== > > Both produce a string of blanks. In the first example, you would need a > %d format specifier to get leading zeros. Did you try them? Ed.
From: John Kelly on 17 Jun 2010 10:12 On Thu, 17 Jun 2010 09:02:37 -0500, Ed Morton <mortonspam(a)gmail.com> wrote: >On 6/17/2010 8:53 AM, John Kelly wrote: >> On Thu, 17 Jun 2010 07:59:31 -0500, Ed Morton<mortonspam(a)gmail.com> >> wrote: >> >>>> $ printf "%020s\n" | tr '0' '=' >>>> ==================== >> >>> ...nor do I know why I thought "0" was a better char for replacement than " ": >>> >>> $ printf "%20s\n" | tr ' ' '=' >>> ==================== >> >> Both produce a string of blanks. In the first example, you would need a >> %d format specifier to get leading zeros. > >Did you try them? Yes: fw:~/temp# bash --version GNU bash, version 3.2.39(1)-release (i486-pc-linux-gnu) Copyright (C) 2007 Free Software Foundation, Inc. fw:~/temp# printf "%020s ta-da\n" ta-da fw:~/temp# printf "%020d ta-da\n" 00000000000000000000 ta-da -- Web mail, POP3, and SMTP http://www.beewyz.com/freeaccounts.php
From: Ed Morton on 17 Jun 2010 10:17 On 6/17/2010 8:53 AM, John Kelly wrote: > On Thu, 17 Jun 2010 07:41:36 -0500, Ed Morton<mortonspam(a)gmail.com> > wrote: > >> Don't know if printf or tr are bash built-ins. Don't know why you'd care though. > > I prefer builtins, to prevent spawning more subshells/pids. Every call > to an external utility is yet another pid. > > Quick and dirty shell hacks have a way of becoming permanent solutions, > so I try to keep them as efficient as possible. Not all shell solutions that use non-builtins are quick and dirty. Efficiency is sometimes important (e.g. if you're writing some code that'll get called many times in a loop), but usually readability, extensibility, etc. are much more important. In this case, it's hard to imagine why you'd want to loop printing N equals signs repeatedly with N varying by iteration (because if it didn't you'd execute the command once and save the result in a variable before the loop) so you'd be better off not worrying about efficiency for this one and just trying to get something concise. Ed.
From: Ed Morton on 17 Jun 2010 10:20 On 6/17/2010 9:12 AM, John Kelly wrote: > On Thu, 17 Jun 2010 09:02:37 -0500, Ed Morton<mortonspam(a)gmail.com> > wrote: > >> On 6/17/2010 8:53 AM, John Kelly wrote: >>> On Thu, 17 Jun 2010 07:59:31 -0500, Ed Morton<mortonspam(a)gmail.com> >>> wrote: >>> >>>>> $ printf "%020s\n" | tr '0' '=' >>>>> ==================== >>> >>>> ...nor do I know why I thought "0" was a better char for replacement than " ": >>>> >>>> $ printf "%20s\n" | tr ' ' '=' >>>> ==================== >>> >>> Both produce a string of blanks. In the first example, you would need a >>> %d format specifier to get leading zeros. >> >> Did you try them? > > Yes: > > fw:~/temp# bash --version > GNU bash, version 3.2.39(1)-release (i486-pc-linux-gnu) > Copyright (C) 2007 Free Software Foundation, Inc. > > fw:~/temp# printf "%020s ta-da\n" > ta-da > > fw:~/temp# printf "%020d ta-da\n" > 00000000000000000000 ta-da > Huh, interesting. You're right, of course, I should have used %d. Don't know why this works: $ bash --version GNU bash, version 3.2.39(20)-release (i686-pc-cygwin) Copyright (C) 2007 Free Software Foundation, Inc. $ printf "%020s ta-da\n" 00000000000000000000 ta-da $ Maybe another cygwin-ism? Ed.
From: John Kelly on 17 Jun 2010 10:30
On Thu, 17 Jun 2010 09:20:59 -0500, Ed Morton <mortonspam(a)gmail.com> wrote: >>>>>> $ printf "%020s\n" | tr '0' '=' >>>>>> ==================== >>>> >>>>> ...nor do I know why I thought "0" was a better char for replacement than " ": >>>>> >>>>> $ printf "%20s\n" | tr ' ' '=' >>>>> ==================== >>>> >>>> Both produce a string of blanks. In the first example, you would need a >>>> %d format specifier to get leading zeros. >>> >>> Did you try them? >> >> Yes: >> >> fw:~/temp# bash --version >> GNU bash, version 3.2.39(1)-release (i486-pc-linux-gnu) >> Copyright (C) 2007 Free Software Foundation, Inc. >> >> fw:~/temp# printf "%020s ta-da\n" >> ta-da >> >> fw:~/temp# printf "%020d ta-da\n" >> 00000000000000000000 ta-da >> > >Huh, interesting. You're right, of course, I should have used %d. Don't know why >this works: > >$ bash --version >GNU bash, version 3.2.39(20)-release (i686-pc-cygwin) >Copyright (C) 2007 Free Software Foundation, Inc. >$ printf "%020s ta-da\n" >00000000000000000000 ta-da >$ > >Maybe another cygwin-ism? Must be a bug. The bash man page, discussing printf, says: > The format is reused as necessary to consume all of the arguments. > If the format requires more arguments than are supplied, the extra > format specifications behave as if a zero value or null string, as > appropriate, had been supplied. So in the case of %s, it will supply a null string, and any "string," null or otherwise, should be padded with blanks, not zeros. -- Web mail, POP3, and SMTP http://www.beewyz.com/freeaccounts.php |