我有一个文件:
$ cat test.txt
a b c
d e f
x y z
我可以用Python来填充列:
import random
with open('test.txt', 'r') as fin:
with open('test-shuffle.txt', 'w') as fout:
for line in fin:
line = line.strip().split('\t')
random.shuffle(line)
fout.write('\t'.join(line) + '\n')
[OUT]:
$ cat test-shuffle.txt
b c a
e d f
x y z
但有没有办法在命令行上执行此操作?也许使用cut
,awk
,sed
等?
另外,如果我只想对特定列进行随机播放,那么命令行上是否也可以这样做,例如如果我只想改变第二和第三列:
import random
with open('test.txt', 'r') as fin:
with open('test-shuffle.txt', 'w') as fout:
for line in fin:
line = line.strip().split('\t')
first , second, third = line
second_third = [second, third]
random.shuffle(second_third)
fout.write('\t'.join([first] + second_third) + '\n')
答案 0 :(得分:5)
你没有提到perl,但perl是cut,awk,sed的超集。这是编程语言的瑞士军队链锯!
$ cat /tmp/test.tsv
a b c
d e f
x y z
$ perl -mList::Util -aln -F'\t' -e 'print join("\t", List::Util::shuffle @F)' < /tmp/test.tsv
b a c
d f e
y x z
$ perl -mList::Util -aln -F'\t' -e 'print join("\t", List::Util::shuffle @F)' < /tmp/test.tsv
c a b
e d f
x z y
哦,第二部分:
$ perl -mList::Util -aln -F'\t' -e 'print join("\t", $F[0], List::Util::shuffle @F[1..2])' < /tmp/test.tsv
a c b
d f e
x y z
答案 1 :(得分:3)
可以使用coreutils完成:
xargs -l shuf -e < test.txt| xargs -l3
答案 2 :(得分:2)
awk
救援!
awk 'NR==1 {srand();
for(i=1;i<=NF;i++) a[i]=i;
for(i=1;i<=NF;i++)
{j=1+int((NF-i)*rand());
t=a[i]; a[i]=a[NF-j]; a[NF-j]=t}}
{for(i=1;i<=NF;i++) printf "%s",$(a[i]) FS;
print ""}'