Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Merging 2 files records

31 views
Skip to first unread message

Niko11

unread,
Jul 25, 2016, 3:02:04 AM7/25/16
to
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?

Lorenz

unread,
Jul 25, 2016, 5:05:36 AM7/25/16
to
Niko11 wrote:
>I am trying to use awk to merge two text files in a rather peculiar way [...]

sounds like a homework assignment 8-)

Have you tried anything at all yet?
--

Lorenz

Kenny McCormack

unread,
Jul 25, 2016, 7:14:26 AM7/25/16
to
In article <7dlbpbhbgf3g39g3c...@4ax.com>,
It is either homework or something so obscure and domain-specific that it
doesn't make sense for any of us to take a shot at it. This is the sort of
problem where the OP needs to take a shot at it, and then ask here for
specific help with specific questions/problems encountered. Basically, he
needs to solve it, with our help as needed.

It also sounds like this isn't the real problem - i.e., this is a proxy
for something else - and it will be a "mission creep" sort of problem. You
know, like this:
1) Solve this thing.
1a) Solved
2) Oh, that's not the real thing, but now solve this other thing.
2a) Solved
3) Repeat...

--
Nov 4, 2008 - the day when everything went
from being Clinton's fault to being Obama's fault.

Janis Papanagnou

unread,
Jul 25, 2016, 7:35:04 AM7/25/16
to
On 25.07.2016 09:00, Niko11 wrote:
> 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. [...]

To access two files in parallel in awk you can use the getline command.
To split a comma separated record you can use the split function.

Janis

Dave Sines

unread,
Jul 25, 2016, 9:35:11 AM7/25/16
to
Niko11 <make...@gmail.com> wrote:
> 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
^^^^^^^^^^^^^^^^^
The second field of the second line of the sample input file (file2,
indicated above) doesn't match the specification.
BEGIN {
g = 2
}

FNR == NR {
if (FNR % g == 1)
a[++i] = $0
else
a[i] = a[i] "\n" $0
next
}

{
gsub(/, */, "\n")
if (++j > i) {
i = j
a[j] = $0
} else
a[j] = a[j] "\n" $0
}

END {
for (j = 1; j <= i; ++j)
print a[j]
}

0 new messages