您应该能够做到这一点,而无需编写简单脚本以外的任何代码(即 bash、Windows 批处理、Powershell 等)。有一些标准工具可以快速处理这类事情。
首先,您有一些包含 200 万到 400 万个数字的文件。处理所有这些文件很困难,因此您要做的第一件事是创建一个已排序的组合文件。简单的方法是将所有文件连接到一个文件中,对其进行排序并删除重复项。例如,使用 GNU/Linux cat 和 sort 命令:
cat file1 file2 file3 file4 > combined
sort -u combined > combined_sort
(-u 删除重复项)
这种方法的问题是您最终会排序一个非常大的文件。图 400 万行,每行 15 个字符,外加换行符,以及近 100 天的文件,您正在使用 7 GB。一整年的数据价值将是 25 GB。这需要很长时间。
因此,请对每个单独的文件进行排序,然后将它们合并:
sort -u file1 >file1_sort
sort -u file2 >file2_sort
...
sort -m -u file1 file2 file3 > combined_sorted
-m 开关合并已排序的文件。
现在您所拥有的是一个排序列表,其中包含您迄今为止看到的所有标识符。您想将今天的文件与该文件进行比较。首先,对今天的文件进行排序:
sort -u today >today_sort
现在,您可以比较文件并仅输出今天文件独有的文件:
comm -2 -3 today_sort combined_sort
-2 表示禁止仅出现在第二个文件中的行,-3 表示禁止两个文件共有的行。因此,您将得到的只是 today_sort 中不存在于 combined_sort 中的行。
现在,如果您打算每天都这样做,那么您需要获取 comm 命令的输出并将其与 combined_sort 合并,以便您明天可以使用该组合文件。这使您不必每天都重建combined_sort 文件。所以:
comm -2 -3 today_sort combined_sort > new_values
然后:
sort -m combined_sort new_values > combined_sort_new
你可能想用日期命名文件,所以你会有combined_sort_20140401和combined_sort_20140402等。
因此,如果您从年初开始并希望每天都这样做,您的脚本将如下所示:
sort -u $todays_file > todays_sorted_file
comm -2 -3 todays_sorted_file $old_combined_sort > todays_uniques
sort -m $old_combined_sort todays_sorted_file > $new_combined_sort
$todays_file、$old_combined_sort 和 $new_combined_sort 是您在命令行上传递的参数。所以,如果脚本被称为“每日”:
daily todays_file.txt all_values_20140101 all_values_20140102