I've got a multi-GB file that has elements in 4 lines, and I would like to have every 4 lines randomized in another file, this is, keeping each four lines grouped and randomize those sets. Is there an easy way to do that?
Tell me more
×
Super User is a question and answer site for
computer enthusiasts and power users. It's 100% free, no registration required.
|
|
If you're using a reasonable new linux/unix distribution,
First, use |
|||||
|
|
This is in Python. I'm sure someone will post a Perl answer too. ;-) #!/usr/bin/python
import random
#Change these to the desired files
infile = "/path/to/input/file"
outfile = "/path/to/output/file"
fh = file(infile)
contents = fh.readlines()
fh.close()
chunked = [contents[i:i+4] for i in xrange(0, len(contents), 4)]
random.shuffle(chunked)
fh = file(outfile, 'w')
for chunk in chunked:
for line in chunk:
fh.write(line)
fh.close()
IANA Programmer so somebody could probably improve this, but I tested it and it works just fine. |
||||
|
|