随机随机播放.txt文件

时间:2016-11-29 21:04:59

标签: bash shell text-files

我有一个.txt个文件,其中有大约800,000封电子邮件,如下所示:

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

我的目标是修改此文件,使其如下所示:

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

我想要的是每行都有随机数量的电子邮件,用逗号或空格分隔。我并不是真的想编写一个程序来执行此操作,因为我听说可以使用某些Shell命令来执行此类工作。这是可能的,如果是这样,我将如何实现这一目标?

2 个答案:

答案 0 :(得分:1)

如果你不介意使用awk,这是一种方法:

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

awk脚本部分很好地打印和评论:

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

答案 1 :(得分:1)

将电子邮件读入bash数组;循环遍历数组并打印每个元素,随机决定输入换行符:

readarray -t emails < emails.txt
for e in "${emails[@]}"
do
  printf "%s " "$e"
  [[ $((RANDOM % 10)) == 0 ]] && echo
done
echo