[email protected]:~/dingxue# cat aa 1234567 abcdefg 利用awk 处理 (1)echo {a..z}利用awk 随机打印一个字母?随机打印N个字母,假设n=3?
(2)随机生成六位数字和字母的随机组合 (3)随机取出以上字符的一列 (4)随机取出以上字符的五列
思路:
随机打印一个数字在awk 中使用rand()函数,但是如果只是用rand();就会出现如下情况,
[email protected]:~/dingxue# awk ‘BEGIN{print rand()}‘
0.237788
[email protected]:~/dingxue# awk ‘BEGIN{print rand()}‘
0.237788
[email protected]:~/dingxue# awk ‘BEGIN{print rand()}‘
0.237788
[email protected]:~/dingxue# awk ‘BEGIN{print rand()}‘
0.237788
每一次都是相同的数字,但是如果使用srand()函数作为一个种子,就可以解决这个问题:
[email protected]:~/dingxue# awk ‘BEGIN{srand();print rand()}‘
0.445072
[email protected]:~/dingxue# awk ‘BEGIN{srand();print rand()}‘
0.150799
代码:
(1) echo {a..z} |awk ‘{srand();a[i]=int(rand()*100%24+1)}{print $a[i]}‘ echo {a..z}| awk ‘{srand();for(i=1;i<=3;i++)a[i]=int(rand()*100%24+1)}{for(i=1;i<=3;i++)printf $a[i];printf RS}‘ (2) echo {a..z}|xargs echo {0..9}| awk ‘{srand();for(i=1;i<=NF;i++)a[i]=int(rand()*100%24+1)}{for(i=1;i<=6;i++)printf $a[i];printf RS}‘ (3) awk -F ‘‘ ‘{srand();a[i]=int(rand()*100%7+1)}{print $a[i]}‘ aa (4) awk -F ‘‘ ‘{srand();for(i=1;i<=NF;i++)a[i]=int(rand()*100%7+1)}{for(i=1;i<=5;i++)printf $a[i];printf RS}‘ aa