我有下面的脚本来处理带有一些数据的文件:首先,头被打印到输出文件中。然后随机选择输入中的60000行,并将其打印到输出(多次打印同一行的可能性是显式所需)。
N = 60000
gawk '{if (NR < 37) print $0}' input > output
MAX=$(gawk 'END{print NR}' input)
for ((i=1; i<=$N; i++ ))
do
declare $(gawk -v min=37 -v max=$MAX -v seed=$RANDOM 'BEGIN{srand(seed); print "x="int(min+rand()*(max-min+1))}')
gawk -v l=$x 'NR>36 && NR==l {print $0}' input >> output
done我现在这是非常低效的,所以我对如何提高这段代码的性能持开放态度,也许可以一直阻止输入文件的打开和关闭?
谢谢您抽时间见我!
发布于 2018-05-07 13:39:15
首先要从名为input的文件中提取36个行头,然后从文件的其余部分中选择60000条随机行,并且有可能多次随机选择同一行。所有输出都应该转到名为output的文件中。
使用GNU中的shuf:
#!/bin/sh
# Fetch header (36 first lines)
head -n 36 output
# Scramble the other lines and pick 60000 (allowing for repeated lines)
tail -n +37 >output另一种选择是:
( head -n 36 output对于GNU head,它将输入文件流保留在最后一行输出后的位置,这意味着shuf可以在head完成读取的地方继续(对于某些非GNU head实现可能不起作用):
( head -n 36; shuf -r -n 60000 ) outputhttps://unix.stackexchange.com/questions/442315
复制相似问题