I am trying to use awk to merge two text files in a rather peculiar way, taking two lines from file1, a group of word(s) from file2 (but placed on a separate line), alternating ad infinitum. Groups of word(s) from file2 are delimited by commas. For example:
file1
A Partridge in a Pear Tree
Two Turtle Doves
Three French Hens
Four Calling Birds
Five Gold Rings
Six Geese a-Laying
Seven Swans a-Swimming
Eight Maids a-Milking
Nine Ladies Dancing
Ten Lords a-Leaping
Eleven Pipers Piping
Twelve Drummers Drumming
file2
I was born, the red planet, I am hungry, on Mars
I love frogs, they are so tasty, with gold sun, red ketchup
Output file
A Partridge in a Pear Tree
Two Turtle Doves
I was born
the red planet
I am hungry
on Mars
Three French Hens
Four Calling Birds
I love frogs
they are so tasty
with gold sun
red ketchup
Five Gold Rings
Six Geese a-Laying
Seven Swans a-Swimming
Eight Maids a-Milking
Nine Ladies Dancing
Ten Lords a-Leaping
Eleven Pipers Piping
Twelve Drummers Drumming
Details:
- In the output file 4 additional lines created from the fields File2 every 2 lines file1
- file1 is split into couplets of two lines, regardless of content
- A line in file2 have 4 number of groups (i.e., 3 number of commas)
- in the output file does not have commas
- A group in file2 have a fixed number of fild
- file1 and file2 may be arbitrarily long
- file2 always less than file1
File2 fields separated by commas always occur in the same order in each record ( 3,3,3,2) ie., $1 $2 $3, $4 $5 $6, $7 $8 $9, $10 S11
In the output file to be so arranged
A Partridge in a Pear Tree
Two Turtle Doves
$1 $2 $3
$4 $5 $6
$7 $8 $9
$10 S11
Three French Hens
Four Calling Birds
I love frogs
they are so tasty
with gold sun
red ketchup
Five Gold Rings
Six Geese a-Laying
Seven Swans a-Swimming
Eight Maids a-Milking
Nine Ladies Dancing
Ten Lords a-Leaping
Eleven Pipers Piping
Twelve Drummers Drumming
- Desired behavior when you reach the end of one file but still have data in the other is unspecified- the remaining data (from file1) will be printed without changes
How do I do this?
cat file1 | paste -d, - - file2 | tr ',' '\n'