Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

A question about awk

21 views
Skip to first unread message

Peter S Tillier

unread,
Aug 30, 2001, 6:05:23 PM8/30/01
to
A few weeks ago I was corresponding with Eric Pement about the sed FAQ,
when I mentioned that I was a fan of awk, Eric asked me this question:

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.


Erik Christiansen

unread,
Aug 30, 2001, 11:08:05 PM8/30/01
to
In article <999209126.17243.0...@news.demon.co.uk>,

Peter S Tillier <pet...@deadspam.com> wrote:
>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++}}"


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

Grod

unread,
Aug 31, 2001, 2:46:21 AM8/31/01
to
"Peter S Tillier" <peter....@btinternet.com> wrote in message news:<999209126.17243.0...@news.demon.co.uk>...
> What's the SHORTEST awk script to print a row of 65 hyphens?

awk 'BEGIN{printf("%65s",x)}' | tr ' ' -

Jurgen Riepe

unread,
Aug 31, 2001, 2:54:52 AM8/31/01
to
"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 "-"}'

Jurgen

Kenny McCormack

unread,
Aug 31, 2001, 9:17:06 AM8/31/01
to
In article <ffd662ea.0108...@posting.google.com>,

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.

Eiso AB

unread,
Aug 31, 2001, 9:11:51 AM8/31/01
to
Peter S Tillier wrote:
>
> A few weeks ago I was corresponding with Eric Pement about the sed FAQ,
> when I mentioned that I was a fan of awk, Eric asked me this question:
>
> 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>

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 \_________________________

Bob Harris

unread,
Aug 31, 2001, 9:48:05 AM8/31/01
to
In article <999209126.17243.0...@news.demon.co.uk>, Peter
S Tillier <peter....@btinternet.com> wrote:

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

Mark Katz

unread,
Aug 31, 2001, 9:57:56 AM8/31/01
to
In article <b0a8f4e1.01083...@posting.google.com>
Jurge...@web.de "Jurgen Riepe" writes:

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


Brian Inglis

unread,
Sep 16, 2001, 7:22:44 PM9/16/01
to
On Fri, 31 Aug 01 13:57:56 GMT, ma...@ispc001.demon.co.uk (Mark
Katz) wrote:

>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

Eiso AB

unread,
Jan 4, 2002, 2:43:09 PM1/4/02
to
This came up some time ago.
http://groups.google.com/groups?q=shortest+65-hyphens

The SHORTEST awk script to print a row of 65 hyphens ?

gawk 'BEGIN{$65=OFS="-";print}'

Eiso

Larry__Weiss

unread,
Jan 4, 2002, 3:06:28 PM1/4/02
to
Eiso AB wrote:
> This came up some time ago.
> http://groups.google.com/groups?q=shortest+65-hyphens
> The SHORTEST awk script to print a row of 65 hyphens ?
> gawk 'BEGIN{$65=OFS="-";print}'
>

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

John DuBois

unread,
Jan 4, 2002, 9:45:42 PM1/4/02
to
In article <5CD1C4DF8CF3B417.173443EB...@lp.airnews.net>,

Larry__Weiss <l...@airmail.net> wrote:
> gawk 'BEGIN{$1000000=OFS=" ";printf("%s",$0)}' >xo
>
>will result in file "xo" containing exactly 1 million space chars.

A slightly shorter version:

gawk 'BEGIN{printf("%1000000s","")}' >xo

John
--
John DuBois spc...@armory.com KC6QKZ/AE http://www.armory.com/~spcecdt/

Peter S Tillier

unread,
Jan 6, 2002, 3:16:25 PM1/6/02
to
Larry__Weiss <l...@airmail.net> wrote:
<snip>

>
> Here's a variation that gives a nice clean screen
> afterwards in an MSDOS or Windows "DOS Prompt" environment:
>
> gawk 'BEGIN{$9999=OFS=" ";print}'
>

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.

Patrice Neff

unread,
Jan 6, 2002, 7:39:40 PM1/6/02
to
pet...@deadspam.com (Peter S Tillier) writes:

> 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

0 new messages