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?

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

2 Answers 2

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 
2
  • You're right, corrected. Commented Jul 25, 2016 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 Commented Jul 25, 2016 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

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.