A question about awk 
Author Message
 A question about awk

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.



Tue, 17 Feb 2004 06:05:23 GMT  
 A question about awk


Quote:
>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)

Quote:
}

   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



Tue, 17 Feb 2004 11:08:05 GMT  
 A question about awk

Quote:
> What's the SHORTEST awk script to print a row of 65 hyphens?

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


Tue, 17 Feb 2004 14:46:21 GMT  
 A question about awk

Quote:
> 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



Tue, 17 Feb 2004 14:54:52 GMT  
 A question about awk

Quote:


>> What's the SHORTEST awk script to print a row of 65 hyphens?

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

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.



Tue, 17 Feb 2004 21:17:06 GMT  
 A question about awk

Quote:

> 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

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


Tue, 17 Feb 2004 21:11:51 GMT  
 A question about awk

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



Tue, 17 Feb 2004 21:48:05 GMT  
 A question about awk


Quote:


>> 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 }'

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



Tue, 17 Feb 2004 21:57:56 GMT  
 A question about awk

Quote:





>>> 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;

Quote:
}

Thanks. Take care, Brian Inglis         Calgary, Alberta, Canada
--

    fake address                use address above to reply

                                                spam traps


Fri, 05 Mar 2004 07:22:44 GMT  
 A question about awk
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

Quote:


> > 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.



Wed, 23 Jun 2004 03:43:09 GMT  
 A question about awk

Quote:

> 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



Wed, 23 Jun 2004 04:06:28 GMT  
 A question about awk

Quote:

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



Wed, 23 Jun 2004 10:45:42 GMT  
 A question about awk
<snip>

Quote:

> 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.



Fri, 25 Jun 2004 04:16:25 GMT  
 A question about awk

Quote:
> 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



Fri, 25 Jun 2004 08:39:40 GMT  
 
 [ 14 post ] 

 Relevant Pages 

1. question about awk and mysql

2. a short cut related question in awk

3. Beginner's question on AWK

4. A question re awk

5. Simple question concerning awk command

6. Stupid questions from awk novice

7. common mistakes in awk: comparing awk with C

8. Awk compilers / Awk to C converters

9. Arrays in awk/awk help please!

10. Help with Awk, totally new to AWK programing

11. awk process in awk ??

12. AWK newbie is looking for a AWK help with his 1st program

 

 
Powered by phpBB® Forum Software