2

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?

  • Are awk is necessary? cat file1 | paste -d, - - file2 | tr ',' '\n' – Costas Jul 24 '16 at 21:26
1

I believe you have put fifth line from the file1 too early in your example.

If I'm right try this snippet:

awk '(NR+1)%2{print $0;getline<"file2";n=split($0,a,", ");if(n>1)for(i in a)print a[i];next}1' file1

output:

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
  • You're right, corrected. – darik2 Jul 25 '16 at 5:53
  • I get such an effect: 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 I was born, the red planet, I am hungry, on Mars I love frogs, they are so tasty, with gold sun, red ketchup they are so tasty with gold sun red ketchup I love frogs – darik2 Jul 25 '16 at 6:00
0

Apart from my comment above if you prefer awk script

awk -F', ' '1;!(NR%2)&&(getline <"file2")>0{$1=$1;print}' OFS='\n' file1

where

  • 1 - synonim of {print $0}:
    • condition = true (1),
    • action if do not indicated = default (print),
    • print without arguments = print $0
  • !(NR%2) - for even lines:
    • NR - Number of Row(record)
    • % - calculates resedue after dividing by 2,
    • ! - reverse result
  • && - logical AND
  • getline <"file2" - read line into $0 from file2 and divide it into fields with FieldSeparator indicated as option -F=', ' and return 1 if success.
  • $1=$1 - trick to apply OutputFieldSeparator: we have to do something with field(s) otherwise the $0 will printed as it is

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.