2016-11-29 112 views
-2

我有一个.txt文件与〜80万封电子邮件,看起来像这样:洗牌.txt文件随机

[email protected] 
[email protected] 
[email protected] 
[email protected] 
[email protected] 
[email protected] 
[email protected] 
. 
. 
. 

我的目标是修改此文件,以便它看起来像这样:

[email protected], [email protected], [email protected], [email protected] 
[email protected], [email protected] 
[email protected], [email protected] 
[email protected], [email protected], [email protected] 
[email protected], [email protected] 
[email protected], [email protected] 
[email protected], [email protected], [email protected] 
. 
. 
. 

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

+0

文件的每一行对应的朋友不同的用户的列表。每个人都有随机数量的朋友。 –

+0

那么为什么不修复每行3或4封电子邮件? – anubhava

+0

我想我可以做到这一点,但是从现实生活的角度来看,这些人中的每一个人都只有4个朋友吗?例如,我可能有4个朋友,但鲍勃可能有10个朋友。 –

回答

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

我不认为这是问题的答案。他肯定希望重复这些电子邮件 - 单身人士可以成为许多人的朋友。即使在这个例子中,电子邮件也会重复。您的脚本会将文件分成随机数量的电子邮件组(每个电子邮件2至4封电子邮件)。但无论如何......它被接受了,所以也许我错了)。 – arturro

+0

该文本没有提及任何有关重复的内容。我在示例输出中看到,但这可能只是一个懒散的书面示例。 – janos

1

阅读电子邮件到一个bash数组;循环通过阵列和打印的每个元素,随机决定输入一个新行:

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

当你想要一些邮件出现在不同的线路,你应该先预处理文件,复制使用$地址((RANDOM%5))。或者: 读取两次emails.txt并使用随机索引从第二个数组添加其他地址。 –

+0

啊,好点 - 我错过了OP想要复制一些电子邮件的想法;我目前的解决方案将地址混合成团块,但只显示一次。 –