I have file with n lines. (Each line refers to a “question”, and
therefore they are labeled Q.1, Q.2, Q.3, ..., Q.n.)
Each line (question) has a “Marks” attribute,
which has the value 2, 3, 4, 5, or 6.
There are n⁄5 lines with each value.
For example: A 10-line file (i.e., n=10) might look like
amol@mypc:~$ cat questions.txt
Q.1 2 Marks
Q.2 5 Marks
Q.3 4 Marks
Q.4 3 Marks
Q.5 6 Marks
Q.6 4 Marks
Q.7 3 Marks
Q.8 2 Marks
Q.9 6 Marks
Q.10 5 Marks
I know I can split this into five homogeneous (i.e., all the same) files with something like
amol@mypc:~$ grep " 2 Marks" questions.txt > questions2Marks.txt
amol@mypc:~$ grep " 3 Marks" questions.txt > questions3Marks.txt
amol@mypc:~$ grep " 4 Marks" questions.txt > questions4Marks.txt
amol@mypc:~$ grep " 5 Marks" questions.txt > questions5Marks.txt
amol@mypc:~$ grep " 6 Marks" questions.txt > questions6Marks.txt
Each of the resulting files will have n⁄5 lines.
I want to do the inverse operation –
i.e., produce a transpose of the above result.
I want to split my questions.txt file
into n⁄5 files: questions1.txt,
questions2.txt, questions3.txt, ..., questionsM.txt
(using M to represent n⁄5) where each file
is five lines long and is heterogeneous (i.e., all different).
questions1.txt should contain
- the first line in
questions.txtwith2 Marks, - the first line in
questions.txtwith3 Marks, - the first line in
questions.txtwith4 Marks, - the first line in
questions.txtwith5 Marks, and - the first line in
questions.txtwith6 Marks,
in that order. questions2.txt should contain the second line of each, etc.
So, for n=10, M obviously is 2. I would want my example questions.txt from above broken down into these two files:
amol@mypc:~$ cat questions1.txt
Q.1 2 Marks
Q.4 3 Marks
Q.3 4 Marks
Q.2 5 Marks
Q.5 6 Marks
amol@mypc:~$ cat questions2.txt
Q.8 2 Marks
Q.7 3 Marks
Q.6 4 Marks
Q.10 5 Marks
Q.9 6 Marks
How can I achieve that using *nix tools (sed, awk, perl, shell script, etc...)?