我想使用bash或awk或其他任何方法来执行以下操作。我有一个看起来像的文件:
/exec/proc1 ...
/exec/proc1 ...
/exec/proc1 ...
/exec/proc1 ...
/exec/proc1 ...
/exec/proc1 ...
如果我说3即(0,1,2)我想生成一个文件:
export DEVICE_ID=0; /exec/proc1 ...
export DEVICE_ID=0; /exec/proc1 ...
export DEVICE_ID=1; /exec/proc1 ...
export DEVICE_ID=1; /exec/proc1 ...
export DEVICE_ID=2; /exec/proc1 ...
export DEVICE_ID=2; /exec/proc1 ...
然后我将其传输到shuf
,这将随机化线条。这是一个穷人的解决方案,可以跨多个CUDA GPU设备实现负载平衡。
答案 0 :(得分:1)
使用awk你可以这样做:
awk -v n=3 '{print "export DEVICE_ID=" (NR-1)%n "; " $0}' file
export DEVICE_ID=0; /exec/proc1 ...
export DEVICE_ID=1; /exec/proc1 ...
export DEVICE_ID=2; /exec/proc1 ...
export DEVICE_ID=0; /exec/proc1 ...
export DEVICE_ID=1; /exec/proc1 ...
export DEVICE_ID=2; /exec/proc1 ...
export DEVICE_ID=0; /exec/proc1 ...
export DEVICE_ID=1; /exec/proc1 ...
export DEVICE_ID=2; /exec/proc1 ...
export DEVICE_ID=0; /exec/proc1 ...
答案 1 :(得分:0)
鉴于你在评论中所说的内容,为什么不让awk生成你最终想要的随机数,例如:
$ awk -v n=3 'BEGIN{srand()} {printf "export DEVICE_ID=%d; %s\n", (n+1)*rand(), $0}' file
export DEVICE_ID=3; /exec/proc1 ...
export DEVICE_ID=3; /exec/proc1 ...
export DEVICE_ID=2; /exec/proc1 ...
export DEVICE_ID=3; /exec/proc1 ...
export DEVICE_ID=0; /exec/proc1 ...
export DEVICE_ID=2; /exec/proc1 ...
$ awk -v n=3 'BEGIN{srand()} {printf "export DEVICE_ID=%d; %s\n", (n+1)*rand(), $0}' file
export DEVICE_ID=3; /exec/proc1 ...
export DEVICE_ID=3; /exec/proc1 ...
export DEVICE_ID=3; /exec/proc1 ...
export DEVICE_ID=0; /exec/proc1 ...
export DEVICE_ID=0; /exec/proc1 ...
export DEVICE_ID=2; /exec/proc1 ...
$ awk -v n=3 'BEGIN{srand()} {printf "export DEVICE_ID=%d; %s\n", (n+1)*rand(), $0}' file
export DEVICE_ID=0; /exec/proc1 ...
export DEVICE_ID=3; /exec/proc1 ...
export DEVICE_ID=1; /exec/proc1 ...
export DEVICE_ID=2; /exec/proc1 ...
export DEVICE_ID=1; /exec/proc1 ...
export DEVICE_ID=1; /exec/proc1 ...
或者如果您需要在输出中始终具有大约相同数量的1s,2s和3s,则可以使用Knuth Shuffle(参见http://rosettacode.org/wiki/Knuth_shuffle#AWK)。
$ cat tst.awk
# Shuffle an _array_ with indexes from 1 to _len_.
function shuffle(array, len, i, j, t) {
for (i = len; i > 1; i--) {
# j = random integer from 1 to i
j = int(i * rand()) + 1
# swap array[i], array[j]
t = array[i]
array[i] = array[j]
array[j] = t
}
}
BEGIN { srand(); for (i=1;i<=n;i++) array[i]=i }
{
idx = (NR-1) % n + 1
if (idx == 1) {
shuffle(array, n)
}
printf "export DEVICE_ID=%d; %s\n", array[idx], $0
}
$ awk -v n=3 -f tst.awk file
export DEVICE_ID=3; /exec/proc1 ...
export DEVICE_ID=1; /exec/proc1 ...
export DEVICE_ID=2; /exec/proc1 ...
export DEVICE_ID=2; /exec/proc1 ...
export DEVICE_ID=3; /exec/proc1 ...
export DEVICE_ID=1; /exec/proc1 ...
$
$ awk -v n=3 -f tst.awk file
export DEVICE_ID=2; /exec/proc1 ...
export DEVICE_ID=3; /exec/proc1 ...
export DEVICE_ID=1; /exec/proc1 ...
export DEVICE_ID=3; /exec/proc1 ...
export DEVICE_ID=2; /exec/proc1 ...
export DEVICE_ID=1; /exec/proc1 ...
$
$ awk -v n=3 -f tst.awk file
export DEVICE_ID=2; /exec/proc1 ...
export DEVICE_ID=3; /exec/proc1 ...
export DEVICE_ID=1; /exec/proc1 ...
export DEVICE_ID=2; /exec/proc1 ...
export DEVICE_ID=1; /exec/proc1 ...
export DEVICE_ID=3; /exec/proc1 ...