Re4XN Re4XN - 11 days ago 7
Bash Question

Shuffle a .txt file randomly

I have a

.txt
file with ~800,000 e-mails that looks like this:

inawalton@yahoo.com
wolf2014@hotmail.com
nutnhuni24@adelphia.net
bluerebel14@aol.com
jake@vcn.com
mguillory@heincpa.com
iamderf@netzero.com
.
.
.


My goal is to modify this file so that it looks like this:

inawalton@yahoo.com, nutnhuni24@adelphia.net, wolf2014@hotmail.com, jake@vcn.com
wolf2014@hotmail.com, bluerebel14@aol.com
nutnhuni24@adelphia.net, mguillory@heincpa.com
bluerebel14@aol.com, inawalton@yahoo.com, jake@vcn.com
jake@vcn.com, bluerebel14@aol.com
mguillory@heincpa.com, iamderf@netzero.com
iamderf@netzero.com, jake@vcn.com, bluerebel14@aol.com
.
.
.


What I want is to have a random amount of emails per line, separated by either commas or spaces. I didn't really want to write a program to do this, since I've heard it is possible to use certain Shell commands to do this kind of work. Is that possible and if so, how would I achieve this?

Answer

If you don't mind using awk, here's one way to do it:

awk 'BEGIN { srand(); } { printf $0; for (i = 0; i <= int(3 * rand()); i++) { if (getline) printf ", " $0; } print ""; }' < input.txt

The awk script part nicely printed and commented:

BEGIN {
  # initialize random seed
  srand();
}
{
  # print the next line, with terminating newline character
  printf $0;

  # loop 1 to 3 times
  for (i = 0; i <= int(3 * rand()); i++) { 
    # if we can successfully read one more line
    if (getline) {
      # print a comma and the next line
      printf ", " $0;
    }
  }

  # print a newline character to complete the line
  print "";
}