I want to write a script that takes text files as arguments and merge them.
- All the files consist of grouped lines divided by empty lines.
- The script can take more than 2 arguments
- The files can include more groups
If the files have different number of groups, when the groups in one file ends, the remaining groups in the other file should be added one after another in order like this:
111-211-121-122-221-222-223-131-132-133-231-241-251-261-271
I want to merge the files to stdout and the output should be "1st group from the first file then 1st group from the second file then 2nd group from 1th file..."
Let's say that the lines in the files are formatted as "file-number_group-number_line-number" like this:
01_01_01
01_02_01
01_02_02
01_02_03
01_03_01
02_01_01
02_01_02
02_02_01
02_03_01
02_03_02
Then the output should be:
01_01_01
02_01_01
01_02_01
01_02_02
01_02_03
02_02_01
01_03_01
02_03_01
02_03_02
I tried using paste
but I ran into problems.
#!/bin/bash
paste -d "\n\n" $1 $2 | sed '/^[[:space:]]*$/d'
and the output is this:
01_01_01
02_01_01
02_01_02
01_02_01
01_02_02
02_02_01
01_02_03
02_03_01
01_03_01
02_03_02
Also I want to see which file each line came from so how can I add the file numbers to the beginning of each line like this :
1:01_01_01
2:02_01_01
1:01_02_01
1:01_02_02
1:01_02_03
2:02_02_01
1:01_03_01
2:02_03_01
2:02_03_02