ep> Okay, here's a question I have for you that I'm
ep> tinkering with right now. What's the SHORTEST
ep> awk script to print a row of 65 hyphens?
ep>
ep> Here's what I've come up with, but I don't
ep> think it's the best:
ep>
ep> echo - | awk "{while(i<65){printf $1;i++}}"
ep>
ep> It's not exactly perl, which could say:
ep>
ep> perl -e "print \"-\" x 65;"
ep>
ep> but I'm interested in a GNU awk solution.
ep> Any ideas?
I came up with a few alternatives, but none actually significantly
shorter than Eric's above.
I can get a similar length program to perl in REXX;
rexx 'say substr("",1,65,"-")'
but I'd be interested to see if we can come up with a considerably
shorter version for gawk. A pity that gawk's substr doesn't work a bit
more like rexx's.
Thoughts anyone?
Regards,
Peter
--
Peter S Tillier
"Who needs perl when you can write dc in sed?"
peter dot tillier at btinternet dot com
To reply direct to me please use the above address
not the "Reply To" which activates a spam trap.
Peter,
In real life we can call a function: repeat("-",65)
function repeat(x,n)
{ while (i < n)
printf("%s",x)
}
making each invocation suitably short.
But we can presumably also use gawk's new extension facility to incorporate
the function in the tool, if one of the rules in this game is to minimise
even a single invocation. :-)
Erik
--
All the really good ideas I ever had came to me while I was milking a cow.
-- Grant Wood
awk 'BEGIN{printf("%65s",x)}' | tr ' ' -
Steps of the evolution:
echo - | awk "{while(i<65){printf $1;i++}}"
echo - | awk "{while(i++<65){printf $1}}"
awk 'BEGIN{printf "%065s",0}'| tr 0 -
pure awk, 14% less than the echo/awk version above, but still not very nice:
awk 'BEGIN{while(i++<65)printf "-"}'
Jurgen
tawk 'BEGIN {print strdup("-",65)}'
gawk 'BEGIN {$0=sprintf("%65s",x);gsub(/ /,"-");print}'
Note that the later technique (using sprintf to build up the length -
padding with spaces - and then using gsub to change the spaces into the
char you want) is how I implement strdup in AWKs that don't have it.
this one's a bit shorter
echo - | awk '{while(i++<64)$0=$0"-"}1'
though I don't like the "echo - |"
this one's even shorter, but cheats a bit
gawk --v | gawk '/n;/&&gsub(".","-")'
it might only work with gawk 3.1.0 ;-)
E
> ep> It's not exactly perl, which could say:
> ep>
> ep> perl -e "print \"-\" x 65;"
> ep>
> ep> but I'm interested in a GNU awk solution.
> ep> Any ideas?
>
> I came up with a few alternatives, but none actually significantly
> shorter than Eric's above.
>
> I can get a similar length program to perl in REXX;
>
> rexx 'say substr("",1,65,"-")'
>
> but I'd be interested to see if we can come up with a considerably
> shorter version for gawk. A pity that gawk's substr doesn't work a bit
> more like rexx's.
>
> Thoughts anyone?
>
> Regards,
> Peter
> --
> Peter S Tillier
> "Who needs perl when you can write dc in sed?"
> peter dot tillier at btinternet dot com
> To reply direct to me please use the above address
> not the "Reply To" which activates a spam trap.
--
_________
_______________________________/ Eiso AB \_________________________
X A few weeks ago I was corresponding with Eric Pement about the sed FAQ,
X when I mentioned that I was a fan of awk, Eric asked me this question:
X
X ep> Okay, here's a question I have for you that I'm
X ep> tinkering with right now. What's the SHORTEST
X ep> awk script to print a row of 65 hyphens?
X ep>
X ep> Here's what I've come up with, but I don't
X ep> think it's the best:
X ep>
X ep> echo - | awk "{while(i<65){printf $1;i++}}"
X ep>
X ep> It's not exactly perl, which could say:
X ep>
X ep> perl -e "print \"-\" x 65;"
X ep>
X ep> but I'm interested in a GNU awk solution.
X ep> Any ideas?
X
X I came up with a few alternatives, but none actually significantly
X shorter than Eric's above.
X
X I can get a similar length program to perl in REXX;
X
X rexx 'say substr("",1,65,"-")'
awk 'BEGIN{ s=sprintf("%65s",""); gsub(".","-",s); print s }'
Not as short all that short, but what the heck.
Bob Harris
Pure awk, a bit longer and perhaps less 'not very nice' ;-))
awk 'BEGIN{x="-------------"; print x x x x x }'
Mark
---
Mark Katz
Mark-IT, London. Delivering MR-IT/Internet solutions
Tel: (44) 20-8731 7516, Fax: (44) 20-8458 9554; http://www.mark-it.co.uk
>In article <b0a8f4e1.01083...@posting.google.com>
> Jurge...@web.de "Jurgen Riepe" writes:
>
>>"Peter S Tillier" <peter....@btinternet.com> wrote in message
>> news:<999209126.17243.0...@news.demon.co.uk>...
>>> ep> Here's what I've come up with, .........
>>> ep>
>>> ep> echo - | awk "{while(i<65){printf $1;i++}}"
>>> ep>
>>
>>Steps of the evolution:
>>
>>echo - | awk "{while(i<65){printf $1;i++}}"
>>echo - | awk "{while(i++<65){printf $1}}"
>>awk 'BEGIN{printf "%065s",0}'| tr 0 -
>>
>>pure awk, 14% less than the echo/awk version above, but still not very nice:
>>
>>awk 'BEGIN{while(i++<65)printf "-"}'
>
>Pure awk, a bit longer and perhaps less 'not very nice' ;-))
> awk 'BEGIN{x="-------------"; print x x x x x }'
>
The general approach to string repeats is, in awk:
function repeat(s,n)
{
n *= length( s ); # multiply n to length
while (length( s ) < n)
{
s = s s; # double length
}
s = substr( s, 1, n); # chop back as required
return s;
}
Thanks. Take care, Brian Inglis Calgary, Alberta, Canada
--
Brian....@CSi.com (Brian dot Inglis at SystematicSw dot ab dot ca)
fake address use address above to reply
tos...@aol.com ab...@aol.com ab...@yahoo.com ab...@hotmail.com ab...@msn.com ab...@sprint.com ab...@earthlink.com ab...@cadvision.com ab...@ibsystems.com u...@ftc.gov
spam traps
The SHORTEST awk script to print a row of 65 hyphens ?
gawk 'BEGIN{$65=OFS="-";print}'
Eiso
Clever!
Here's a variation that gives a nice clean screen
afterwards in an MSDOS or Windows "DOS Prompt" environment:
gawk 'BEGIN{$9999=OFS=" ";print}'
GAWK allows this pattern to create files populated to exact sizes:
gawk 'BEGIN{$1000000=OFS=" ";printf("%s",$0)}' >xo
will result in file "xo" containing exactly 1 million space chars.
- Larry Weiss
A slightly shorter version:
gawk 'BEGIN{printf("%1000000s","")}' >xo
John
--
John DuBois spc...@armory.com KC6QKZ/AE http://www.armory.com/~spcecdt/
But note that this type of thing may fail on some awks that are
limited in the number of fields that are "assignable." It would
be good if all awks supported unlimited fields.
<snip>
Peter
--
To email direct use peter dot tillier at btinternet dot com
not the "reply to"
--
Sent using an unregistered copy of RMRNews v1.02
Check out our website at http://www.rmrsoft.com/
for other high quality software for EPOC machines.
> But note that this type of thing may fail on some awks that are
> limited in the number of fields that are "assignable." It would
> be good if all awks supported unlimited fields.
Yep.
$ mawk 'BEGIN{$99999=OFS=" ";print}'
mawk: line 1: $99999 exceeds maximum field(32767)
what a pity.
patrice