如果我有一个以下格式的文本文件,那里是字段分隔符,文本分隔符是'这是在文本编辑器(如gedit
)中打开时的外观'not','here and stuff','overthere other stuff not blah'
'cookies','no cookies in the cookie jar','I must have coffie'
'what','do you want','I'm busy'
'more,working on stuff','tired of owrking on stuff'
'ok','I got a new mugg','I have no clothes'
'maybe','this is','enough sample input'
我希望改变$ 2和$ 3出现的顺序,以便它随机,但我想单独留下1美元,我想确保给定行上出现的所有内容都保留在行,我该怎么做?
sort --random-sort
之类的东西可以随机化行的顺序,但第二列和第三列中出现的顺序又如何呢?
样本输出(由我随机伪造)
'not','overthere other stuff not blah','here and stuff'
'cookies','no cookies in the cookie jar','I must have coffie'
'what','I'm busy','do you want'
'more','tired of owrking on stuff','working on stuff'
'ok','I got a new mugg','I have no clothes'
'maybe','enough sample input','this is'
答案 0 :(得分:1)
awk中的这样的东西似乎可以按你的意愿工作:
awk -F ',' -v seed=$RANDOM 'BEGIN {srand(seed); OFS=","} {if (int(rand()*100) % 2 == 0)print $1,$2,$3; else print $1,$3,$2 }'
我们首先告诉awk分隔符是","通过-F','
然后,我们获得一个随机种子,通过-v seed=$RANDOM
在开始区块中,我们播种随机数并制作OFS","通过BEGIN {srand(seed); OFS=","}
然后我们得到一个随机数,使它成为一个整数,然后看它的模2是否为0.如果是,打印正常的顺序,否则切换顺序。
根据这项不起作用的意见,这里有一些输入和输出示例:
~$ cat testawk.txt
1,2,3
1,2,3
1,2,3
1,2,3
1,2,3
1,2,3
1,2,3
1,2,3
1,2,3
1,2,3
输出:
~$ awk -F ',' -v seed=$RANDOM 'BEGIN {srand(seed); OFS=","} {if (int(rand()*100) % 2 == 0)print $1,$2,$3; else print $1,$3,$2 }' testawk.txt
1,2,3
1,2,3
1,2,3
1,2,3
1,3,2
1,2,3
1,3,2
1,2,3
1,2,3
1,3,2
这是另一次使用您的数据:
~$ cat testawk2.txt
not,here and stuff,overthere other stuff not blah
cookies,no cookies in the cookie jar,I must have coffie
what,do you want,I'm busy
more,working on stuff,tired of owrking on stuff
ok,I got a new mugg,I have no clothes
maybe,this is,enough sample input
~$ awk -F ',' -v seed=$RANDOM 'BEGIN {srand(seed); OFS=","} {if (int(rand()*100) % 2 == 0)print $1,$2,$3; else print $1,$3,$2 }' testawk2.txt
not,here and stuff,overthere other stuff not blah
cookies,no cookies in the cookie jar,I must have coffie
what,I'm busy,do you want
more,tired of owrking on stuff,working on stuff
ok,I got a new mugg,I have no clothes
maybe,this is,enough sample input
又一次证明每次都会有所不同:
~$ awk -F ',' -v seed=$RANDOM 'BEGIN {srand(seed); OFS=","} {if (int(rand()*100) % 2 == 0)print $1,$2,$3; else print $1,$3,$2 }' testawk2.txt
not,overthere other stuff not blah,here and stuff
cookies,no cookies in the cookie jar,I must have coffie
what,do you want,I'm busy
more,working on stuff,tired of owrking on stuff
ok,I got a new mugg,I have no clothes
maybe,this is,enough sample input