I would like to split a file to different files based on the information in the first line. For example, I have:
Input:
1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 3 3 3 3 3 3 4 4 4 4 4 4 4 30 30 30 30
0 2 2 0 2 0 2 0 2 0 2 2 0 0 2 2 2 0 1 1 1 2 0 2 0 0 0 2 0 0 2 0 2
0 2 1 0 1 0 1 1 1 0 2 2 0 0 2 2 2 0 0 0 0 2 0 2 0 0 1 2 0 0 2 0 2
0 2 1 0 1 0 1 1 1 0 2 2 0 0 2 2 2 0 0 0 0 2 0 2 0 0 1 2 0 0 2 0 2
Desired output:
output1.txt
02202020
02101011
02101011
output2.txt
2022002
1022002
1022002
output3.txt
220111
220000
220000
output4.txt
202000200202
202001200202
202001200202
output30.txt
0202
0202
0202
Best Answer
Perl script.
Set the file name in
$in
in place ofgenome.txt
or give the name as argument.Name the script
counter.pl
and give it executable rights, and run it as./counter.pl
or alternatively
counter.pl:
Fixed the numbers of words per line (was sometimes 32, sometimes 33 in the example).
This version can accommodate any variation of columns, but all lines have to have the same numbers of words. An error will occur (the
die
lines) if the number of words is different, or if it cannot open files.Just adjust the file name (
$in
).Input file: (removed the extra 0 near the end)
output1.txt
output2.txt
output30.txt
output3.txt
output4.txt