Random 每隔一行向Sed\a添加一个随机变量

Random 每隔一行向Sed\a添加一个随机变量,random,sed,windows-subsystem-for-linux,Random,Sed,Windows Subsystem For Linux,我想为sed创建的每一行添加一个随机数 目前我的命令是: sed "a path/to/file$(($RANDOM%7))" tmp/line.txt 结果: line1 path/to/file1 line3 path/to/file1 line5 path/to/file1 line7 path/to/file1 ...(22K lines) 预期: line1 path/to/file1 line3 path/to/file0 line5 path/to/file7 line7 pa

我想为sed创建的每一行添加一个随机数

目前我的命令是:

sed "a path/to/file$(($RANDOM%7))" tmp/line.txt
结果:

line1
path/to/file1
line3
path/to/file1
line5
path/to/file1
line7
path/to/file1
...(22K lines)
预期:

line1
path/to/file1
line3
path/to/file0
line5
path/to/file7
line7
path/to/file5
...(22k lines)

使用shuf和GNU sed:

shuf -r -e 'path/to/file'{0..7} | sed 'R /dev/stdin' file
或者shuf和awk:

shuf -r -i 0-7 | awk '1; { getline < "/dev/stdin"; print "path/to/file" $0 }' file

变量不使用单引号展开,请尝试双引号。
awk
更适合这种需求,因为
awk
确实有自己的
rand()
函数。
awk-v seed=$(date+%s)'BEGIN{srand(seed);}/path\/to\/file$/{sub RS(//),print$0 int(rand()*7)}'tmp/line.txt
这可能会有所帮助:
seq 14 | sed-e“s/*/bash-c'echo\$((\${RANDOM}%7))”/e“
sed速度较慢,awk看起来更复杂。谢谢,我两个都用。
awk 'BEGIN { srand() } 1; { print "path/to/file" int(rand() * 8) }' file