nawk -> gawk/mawk 
Author Message
 nawk -> gawk/mawk

nawk 'BEGIN {r=0;print ""} {s[++r]=$0} END {srand;print s[int (rand * r)];exit}'

How this line would look in gawk or mawk? It works now only nawk.

Thanks, Ari
--
Ari Kukkonen - Enjoy daily http://www.*-*-*.com/
To fall into a habit is to begin to cease to be.
                    - Miguel de Unamuno, "The Tragic Sense of Life", 1913



Mon, 26 Mar 2001 03:00:00 GMT  
 nawk -> gawk/mawk

Quote:

>nawk 'BEGIN {r=0;print ""} {s[++r]=$0} END {srand;print s[int (rand * r)];exit}'

>How this line would look in gawk or mawk? It works now only nawk.

gawk 'BEGIN {r=0;print ""}
            {s[++r]=$0}
      END {srand();print s[int( (rand() * r) +1)]}' infile

I took the liberty of correcting your logic as what you wrote would
possibly try to access s[0] and never try to access s[r].  The + 1
fixes that.  Awk/gawk is fussy that the parenthesis for a function
be right next to the function name (i.e. no spaces in between).

Both functions (srand and rand) require the parentheses in awk/gawk.
There also seemed to me no reason for the exit, so I dropped it.
I'm unfamiliar with mawk peculiarities.

Chuck Demas
Needham, Mass.

--
  Eat Healthy    |   _ _   | Nothing would be done at all,

  Die Anyway     |    v    | That no one could find fault with it.



Mon, 26 Mar 2001 03:00:00 GMT  
 nawk -> gawk/mawk

Quote:

>nawk 'BEGIN {r=0;print ""} {s[++r]=$0} END {srand;print s[int (rand * r)];exit}'

>How this line would look in gawk or mawk? It works now only nawk.

Some error messages would be nice...

Quote:

>Thanks, Ari

Possibly a multi line issue.  BEGIN and END should be on separate
lines.  Try this:

nawk 'BEGIN {r=0;print ""} \
{s[++r]=$0} \
END {srand;print s[int (rand * r)];exit}' filename

The exit statement does not get you anything in this context.
Also, srand and rand are functions, try writing then as srand() and
rand().

Try writing it as a proper awk program, and run it by typing

    nawk -f myprog.awk filename

That way, you can tell whether its an awk language issue which is the
problem, or a shell quoting issue.  Once the program works, then you
can jam it all into one line if you want.

--
Steve



Mon, 26 Mar 2001 03:00:00 GMT  
 nawk -> gawk/mawk

Quote:

> nawk 'BEGIN {r=0;print ""} {s[++r]=$0} END {srand;print s[int (rand * r)];exit}'

> How this line would look in gawk or mawk? It works now only nawk.

  gawk '{ s[++r] = $0 } END { srand(); print s[int(r * rand()) + 1] }'

For awk scripts generally, and for one-liners especially, you don't need
to initialize numeric variables to 0. It's good awk programming style to
exploit the automatic initialization of variables, particularly for
things such as counters that naturally start at 1 (++n, where n = 0).

In this case, however, the variable r is duplicating work that awk is
already doing automatically; namely, counting input records. This is
better:

  gawk '{ s[NR] = $0 } END { srand(); print s[int(NR * rand()) + 1] }'

If you _must_ print an initial blank line, just add a "\n" to the print
statement:

  gawk '{ s[NR] = $0 } END { srand(); print "\n" s[int(NR * rand()) + 1] }'

As Chuck Demas has already pointed out, the idiom for generating a
random integer k between 1 and n (where 1 <= k <= n, n is an integer)
is

  k = int(n * rand()) + 1

--
Jim Monty

Tempe, Arizona USA                                            END IRONY!



Tue, 27 Mar 2001 03:00:00 GMT  
 nawk -> gawk/mawk

Quote:

>nawk 'BEGIN {r=0;print ""}{s[++r]=$0} END {srand;print s[int (rand * r)];exit}'

You have already been given many ideas on making this work syntactically

However it seems a long way of doing it - especially if the
file is very large.

If all you are trying to do is print out a random line in the file
why not
-  do a wc first to find the number of lines
-  work out a random line number on the first record
-  print and exit when you reach that number

wc file | awk 'xxxx' - file

{
 if (NR == 1) {srand(); rno=int(rand() * $1+0.5); next}
 if (NR-1 == rno) {print; exit}

Quote:
}

Note this awk has not been tested...

Mark
--
Mark Katz
ISPC, London - Innovation in data-delivery tools
Tel: (44) 181-455 4665, Fax (44) 181-458 9554
** See our website at http://www.efiche.com **



Tue, 27 Mar 2001 03:00:00 GMT  
 nawk -> gawk/mawk
<snip>
Quote:
> If all you are trying to do is print out a random line in the file
> why not
> -  do a wc first to find the number of lines
> -  work out a random line number on the first record
> -  print and exit when you reach that number

<snip>

Well, there is an efficiency issue here, although its unclear which way
in actual practice it would fall:

The original program only makes 1 scan thru the file, while using wc
will take 1+ scans

Of course, the overhead of buffering the file in memory may negate that



Tue, 27 Mar 2001 03:00:00 GMT  
 nawk -> gawk/mawk


Quote:
>wc file | awk 'xxxx' - file

>{
> if (NR == 1) {srand(); rno=int(rand() * $1+0.5); next}
> if (NR-1 == rno) {print; exit}
>}

This would be the correct idiom if int() rounded, but it truncates, so
this will generate errors at the ends.  A previous poster quoted the
correct idiom, k = int(rand() * n) + 1, but otherwise this certainly
seems faster.

Mike Ryan

m r y a n  a t  p r o l i f i c s  d o t  remove_spaces c o m



Fri, 30 Mar 2001 03:00:00 GMT  
 
 [ 7 post ] 

 Relevant Pages 

1. RS problem in nawk (it works with gawk and mawk)

2. Differences between awk/nawk/gawk/mawk

3. Gawk bug, gawk won't nawk.

4. help for new user looking to use awk nawk or mawk

5. mawk versus gawk performance

6. dos & os/2 mawk and gawk

7. gawk/mawk/awk Conflict re OFMT?

8. macperl, gawk, and mawk recompared

9. mawk and gawk: Comparing and Contrasting 2 fine programs

10. awk, nawk & gawk

11. large nawk program migrating to gawk

12. AWK/NAWK/GAWK questions !

 

 
Powered by phpBB® Forum Software