alvas alvas -4 years ago 144
Python Question

Shuffling columns in a delimited file

I have a file:

$ cat test.txt
a b c
d e f
x y z


I could do this in Python to shuffle the columns:

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


But is there a way to do it on the command line? Maybe with
cut
,
awk
,
sed
, etc.?


Also, if I want to only shuffle specific columns, is that possible on the command line too, e.g. if I only want to shuffle the 2nd and 3rd column:

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')

Answer Source

You didn't mention perl, but perl is a superset of cut, awk, sed. It's the Swiss Army Chainsaw of programming languages!

$ 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

Oh, the second part:

$ 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
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download