在每行新行中向Sed \ a添加随机变量

问题描述 投票:0回答:1

我想在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)
random sed windows-subsystem-for-linux
1个回答
0
投票

使用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
© www.soinside.com 2019 - 2024. All rights reserved.