【发布时间】:2013-11-04 17:04:08
【问题描述】:
我有一个文本文件,其中包含许多附加随机数(0-n 之间)的字符串。例如:
string1
string33
string10
等等。
我希望能够用相同的字符串替换每个字符串,但附加一个随机数(0-n 之间)。例如:
string2
string9
string12
我已经尝试过这个 nawk 脚本,但我只能用它来替换文件中的所有数字。
nawk 'BEGIN{OFS=FS="";srand()}{for(i=1;i<=NF;i++)sub(/[0-9]/,("string")int(10*rand()),$i)}1' infile > outfile
另外,它会替换每个数字,因此两位数最终可能是三或四。例如:
stringstring4
stringstring30
stringstring3string6
谁能帮我得到想要的输出?
【问题讨论】: