1#!/sw/bin/gawk -f
2
3# Usage: scramble.awk file
4#
5# Copyright 1999 Stephan Schulz, schulz@informatik.tu-muenchen.de
6#
7# Read file line by line and output the lines in random order.
8#
9
10
11function get_pid(     tmp)
12{
13   tmp = PROCINFO["pid"];
14   # getline tmp < "/dev/pid";
15   #   close("/dev/pid");
16   if(!tmp)
17   {
18      print "scramble.awk: Cannot get PID ?!?" > "/dev/stderr";
19      exit 1;
20   }
21   return tmp;
22}
23
24function get_hostname(   pipe, tmp)
25{
26   pipe = "hostname";
27   pipe | getline tmp;
28   close(pipe);
29   return tmp;
30}
31
32function get_tmpname(   file)
33{
34   file = "__scramble__" get_hostname() "__" get_pid() "__";
35   return file;
36}
37
38BEGIN{
39   srand(10);
40}
41
42{
43   key = rand();
44   while(array[key])
45   {
46      key = rand();
47   }
48   array[key] = $0;
49}
50
51
52
53END{
54   procid = get_pid();
55   file = get_tmpname();
56   for(i in array)
57   {
58      printf("%1.12f %s\n", i, array[i]) > file ;
59   }
60   close(file);
61   cmd = "sort " file;
62   while ((cmd | getline tmp) > 0)
63   {
64      print substr(tmp,16);
65   }
66   system("rm " file);
67}
68