我想使用 awk 随机 80/20 拆分文件。
我已经阅读并尝试了找到 HERE 的选项,其中提出了如下建议:
$ awk -v N=`cat FILE | wc -l` 'rand()<3000/N' FILE
如果您想要随机选择,效果很好。
但是,是否可以更改此 awk 以将一个文件分成 80/20(或任何其他)比例的两个文件?
最佳答案
用gawk,你会写
gawk '
BEGIN {srand()}
{f = FILENAME (rand() <= 0.8 ? ".80" : ".20"); print > f}
' file
例子:
seq 100 > 100.txt
gawk 'BEGIN {srand()} {f = FILENAME (rand() <= 0.8 ? ".80" : ".20"); print > f}' 100.txt
wc -l 100.txt*
100 100.txt
23 100.txt.20
77 100.txt.80
200 total
关于特定比例的随机分割文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19770404/