Stack Exchange Network

Stack Exchange network consists of 175 Q&A communities including Stack Overflow, the largest, most trusted online community for developers to learn, share their knowledge, and build their careers.

Visit Stack Exchange
6 Guessed at what the OP meant; illustrated clear presentation style, illuminating unspecified areas.
source | link

Insert a Merge two files: two lines at specific, partial line number with awk, two lines, partial line, etc

Hello I am trying to use awkawk to do thismerge 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:

File1file1

1Linux aaa aaaa
2Solaris bb bbb
3Cygwin cccc cc
4Ubuntu dd dddd
5Fedora ee eeee
6Aix fff fff
7Hpux gggg gg
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
Once upon a midnight dreary, while I pondered, weak and weary,
Over many a quaint and curious volume of forgotten lore—
While I nodded, nearly napping, suddenly there came a tapping,
As of some one gently rapping, rapping at my chamber door.
“’Tis some visitor,” I muttered, “tapping at my chamber door—
            Only this and nothing more.”

File2file2

We were born, in the earth, beyond the land
I was born, on Mars, the red planet
I love frogs, they are so tasty, with, ketchup, I am hungry
1Linux aaa aaaa
2Solaris bb bbb
We were born
3Cygwin cccc cc
4Ubuntu dd dddd
in the earth
5Fedora ee eeee
6Aix fff fff
beyond the land
7Hpux gggg gg
A Partridge in a Pear Tree
Two Turtle Doves
I was born
Three French Hens
Four Calling Birds
on Mars
Five Gold Rings
Six Geese a-Laying
the red planet
Seven Swans a-Swimming
Eight Maids a-Milking
I love frogs
Nine Ladies Dancing
Ten Lords a-Leaping
they are so tasty
Eleven Pipers Piping
Twelve Drummers Drumming
with
Once upon a midnight dreary, while I pondered, weak and weary,
Over many a quaint and curious volume of forgotten lore—
ketchup
While I nodded, nearly napping, suddenly there came a tapping,
As of some one gently rapping, rapping at my chamber door.
I am hungry
“’Tis some visitor,” I muttered, “tapping at my chamber door—
            Only this and nothing more.”

Details:

  • file1 is split into couplets of two lines, regardless of content
  • A line in file2 may have any number of groups (i.e., any number of commas)
  • A group in file2 may have any number of words (including zero???)
  • file1 and file2 may be arbitrarily long
  • Desired behavior when you reach the end of one file but still have data in the other is unspecified.

How do I do this?

Thanks for fast reaction:). I asked a question inaccurately.

Scott asked : Is file1 split after every two lines, based on some content of the file, pre-specified line numbers, or something else?  Is file2 split after every three words, based on the commas, based on some content of the words, or something else?

File1 is split after every two lines pre-specified line numbers.
File2 split after every three words, based on the commas.

Scott asked : Is it guaranteed that file2 will always have exactly one line?

Unfortunately not. file1 and file2 have a random number of lines.

Insert a lines at specific line number with awk

Hello I am trying to use awk to do this.

File1

1Linux aaa aaaa
2Solaris bb bbb
3Cygwin cccc cc
4Ubuntu dd dddd
5Fedora ee eeee
6Aix fff fff
7Hpux gggg gg

File2

We were born, in the earth, beyond the land
1Linux aaa aaaa
2Solaris bb bbb
We were born
3Cygwin cccc cc
4Ubuntu dd dddd
in the earth
5Fedora ee eeee
6Aix fff fff
beyond the land
7Hpux gggg gg

How do I do this?

Thanks for fast reaction:). I asked a question inaccurately.

Scott asked : Is file1 split after every two lines, based on some content of the file, pre-specified line numbers, or something else?  Is file2 split after every three words, based on the commas, based on some content of the words, or something else?

File1 is split after every two lines pre-specified line numbers.
File2 split after every three words, based on the commas.

Scott asked : Is it guaranteed that file2 will always have exactly one line?

Unfortunately not. file1 and file2 have a random number of lines.

Merge two files: two lines, partial line, two lines, partial line, etc

Hello 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
Once upon a midnight dreary, while I pondered, weak and weary,
Over many a quaint and curious volume of forgotten lore—
While I nodded, nearly napping, suddenly there came a tapping,
As of some one gently rapping, rapping at my chamber door.
“’Tis some visitor,” I muttered, “tapping at my chamber door—
            Only this and nothing more.”

file2

I was born, on Mars, the red planet
I love frogs, they are so tasty, with, ketchup, I am hungry
A Partridge in a Pear Tree
Two Turtle Doves
I was born
Three French Hens
Four Calling Birds
on Mars
Five Gold Rings
Six Geese a-Laying
the red planet
Seven Swans a-Swimming
Eight Maids a-Milking
I love frogs
Nine Ladies Dancing
Ten Lords a-Leaping
they are so tasty
Eleven Pipers Piping
Twelve Drummers Drumming
with
Once upon a midnight dreary, while I pondered, weak and weary,
Over many a quaint and curious volume of forgotten lore—
ketchup
While I nodded, nearly napping, suddenly there came a tapping,
As of some one gently rapping, rapping at my chamber door.
I am hungry
“’Tis some visitor,” I muttered, “tapping at my chamber door—
            Only this and nothing more.”

Details:

  • file1 is split into couplets of two lines, regardless of content
  • A line in file2 may have any number of groups (i.e., any number of commas)
  • A group in file2 may have any number of words (including zero???)
  • file1 and file2 may be arbitrarily long
  • Desired behavior when you reach the end of one file but still have data in the other is unspecified.

How do I do this?

5 deleted 801 characters in body
source | link

Hello I am trying to use awk to do this.

File1

1Linux aaa aaaa
2Solaris bb bbb
3Cygwin cccc cc
4Ubuntu dd dddd
5Fedora ee eeee
6Aix fff fff
7Hpux gggg gg

File2

We were born, in the earth, beyond the land

Output file

1Linux aaa aaaa
2Solaris bb bbb
We were born
3Cygwin cccc cc
4Ubuntu dd dddd
in the earth
5Fedora ee eeee
6Aix fff fff
beyond the land
7Hpux gggg gg

How do I do this?

Thanks for fast reaction:). I asked a question inaccurately.

Scott asked : Is file1 split after every two lines, based on some content of the file, pre-specified line numbers, or something else?  Is file2 split after every three words, based on the commas, based on some content of the words, or something else?

File1 is split after every two lines pre-specified line numbers.
File2 split after every three words, based on the commas.

Scott asked : Is it guaranteed that file2 will always have exactly one line?

Unfortunately not. file1 and file2 have a random number of lines.

How to modify the code in order to create the output file?
File1

1Linux aaa aaaa
2Solaris bb bbb
3Cygwin cccc cc
4Ubuntu dd dddd
5Fedora ee eeee
6Aix fff fff
7Hpux gggg gg

File2

We were born, in the earth1, beyond the land, Paris11
I love you, ich liebe dich2, ich mag dich, 22London
I am hungry3, ich habe durst, I am foolish, Berlin33

Output file

1Linux aaa aaaa
2Solaris bb bbb
We were born
in the earth1
beyond the land
Paris11
3Cygwin cccc cc
4Ubuntu dd dddd
I love you
ich liebe dich2
ich mag dich
22London
5Fedora ee eeee
6Aix fff fff
I am hungry3
ich habe durst
I am foolish
Berlin33
7Hpux gggg gg

Thank you for your help

Hello I am trying to use awk to do this.

File1

1Linux aaa aaaa
2Solaris bb bbb
3Cygwin cccc cc
4Ubuntu dd dddd
5Fedora ee eeee
6Aix fff fff
7Hpux gggg gg

File2

We were born, in the earth, beyond the land

Output file

1Linux aaa aaaa
2Solaris bb bbb
We were born
3Cygwin cccc cc
4Ubuntu dd dddd
in the earth
5Fedora ee eeee
6Aix fff fff
beyond the land
7Hpux gggg gg

How do I do this?

Thanks for fast reaction:). I asked a question inaccurately.

Scott asked : Is file1 split after every two lines, based on some content of the file, pre-specified line numbers, or something else?  Is file2 split after every three words, based on the commas, based on some content of the words, or something else?

File1 is split after every two lines pre-specified line numbers.
File2 split after every three words, based on the commas.

Scott asked : Is it guaranteed that file2 will always have exactly one line?

Unfortunately not. file1 and file2 have a random number of lines.

How to modify the code in order to create the output file?
File1

1Linux aaa aaaa
2Solaris bb bbb
3Cygwin cccc cc
4Ubuntu dd dddd
5Fedora ee eeee
6Aix fff fff
7Hpux gggg gg

File2

We were born, in the earth1, beyond the land, Paris11
I love you, ich liebe dich2, ich mag dich, 22London
I am hungry3, ich habe durst, I am foolish, Berlin33

Output file

1Linux aaa aaaa
2Solaris bb bbb
We were born
in the earth1
beyond the land
Paris11
3Cygwin cccc cc
4Ubuntu dd dddd
I love you
ich liebe dich2
ich mag dich
22London
5Fedora ee eeee
6Aix fff fff
I am hungry3
ich habe durst
I am foolish
Berlin33
7Hpux gggg gg

Thank you for your help

Hello I am trying to use awk to do this.

File1

1Linux aaa aaaa
2Solaris bb bbb
3Cygwin cccc cc
4Ubuntu dd dddd
5Fedora ee eeee
6Aix fff fff
7Hpux gggg gg

File2

We were born, in the earth, beyond the land

Output file

1Linux aaa aaaa
2Solaris bb bbb
We were born
3Cygwin cccc cc
4Ubuntu dd dddd
in the earth
5Fedora ee eeee
6Aix fff fff
beyond the land
7Hpux gggg gg

How do I do this?

Thanks for fast reaction:). I asked a question inaccurately.

Scott asked : Is file1 split after every two lines, based on some content of the file, pre-specified line numbers, or something else?  Is file2 split after every three words, based on the commas, based on some content of the words, or something else?

File1 is split after every two lines pre-specified line numbers.
File2 split after every three words, based on the commas.

Scott asked : Is it guaranteed that file2 will always have exactly one line?

Unfortunately not. file1 and file2 have a random number of lines.

    Post Closed as "unclear what you're asking" by Scott, Jeff Schaller, Archemar, dhag, MelBurslan
4 added 68 characters in body
source | link

Scott asked : Is file1 split after every two lines, based on some content of the file, pre-specified line numbers, or something else?  Is file2 split after every three words, based on the commas, based on some content of the words, or something else?

Scott asked : Is file1 split after every two lines, based on some content of the file, pre-specified line numbers, or something else?  Is file2 split after every three words, based on the commas, based on some content of the words, or something else?

File1 is split after every two lines pre-specified line numbers. 
File2 split after every three words, based on the commas.

Scott asked : Is it guaranteed that file2 will always have exactly one line?

Scott asked : Is it guaranteed that file2 will always have exactly one line?

Unfortunately not. file 1file1 and file2 have a random number of lines. 

How to modify the code in order to create the output file? 
File1

1Linux aaa aaaa

2Solaris bb bbb

3Cygwin cccc cc

4Ubuntu dd dddd

5Fedora ee eeee

6Aix fff fff

7Hpux gggg gg

File2

We were born, in the earth1, beyond the land, Paris11

I love you, ich liebe dich2, ich mag dich, 22London

I am hungry3, ich habe durst, I am foolish, Berlin33

Output file

1Linux aaa aaaa

2Solaris bb bbb

We were born

in the earth1

beyond the land

Paris11

3Cygwin cccc cc

4Ubuntu dd dddd

I love you

ich liebe dich2

ich mag dich

22London

5Fedora ee eeee

6Aix fff fffFile1

I am hungry3

1Linux aaa aaaa
2Solaris bb bbb
3Cygwin cccc cc
4Ubuntu dd dddd
5Fedora ee eeee
6Aix fff fff
7Hpux gggg gg

ich habe durstFile2

I am foolish

We were born, in the earth1, beyond the land, Paris11
I love you, ich liebe dich2, ich mag dich, 22London
I am hungry3, ich habe durst, I am foolish, Berlin33

Berlin33Output file

7Hpux gggg gg

1Linux aaa aaaa
2Solaris bb bbb
We were born
in the earth1
beyond the land
Paris11
3Cygwin cccc cc
4Ubuntu dd dddd
I love you
ich liebe dich2
ich mag dich
22London
5Fedora ee eeee
6Aix fff fff
I am hungry3
ich habe durst
I am foolish
Berlin33
7Hpux gggg gg

Scott asked : Is file1 split after every two lines, based on some content of the file, pre-specified line numbers, or something else?  Is file2 split after every three words, based on the commas, based on some content of the words, or something else?

File1 is split after every two lines pre-specified line numbers. File2 split after every three words, based on the commas.

Scott asked : Is it guaranteed that file2 will always have exactly one line?

Unfortunately not. file 1 and file2 have a random number of lines.

How to modify the code in order to create the output file? File1

1Linux aaa aaaa

2Solaris bb bbb

3Cygwin cccc cc

4Ubuntu dd dddd

5Fedora ee eeee

6Aix fff fff

7Hpux gggg gg

File2

We were born, in the earth1, beyond the land, Paris11

I love you, ich liebe dich2, ich mag dich, 22London

I am hungry3, ich habe durst, I am foolish, Berlin33

Output file

1Linux aaa aaaa

2Solaris bb bbb

We were born

in the earth1

beyond the land

Paris11

3Cygwin cccc cc

4Ubuntu dd dddd

I love you

ich liebe dich2

ich mag dich

22London

5Fedora ee eeee

6Aix fff fff

I am hungry3

ich habe durst

I am foolish

Berlin33

7Hpux gggg gg

Scott asked : Is file1 split after every two lines, based on some content of the file, pre-specified line numbers, or something else?  Is file2 split after every three words, based on the commas, based on some content of the words, or something else?

File1 is split after every two lines pre-specified line numbers. 
File2 split after every three words, based on the commas.

Scott asked : Is it guaranteed that file2 will always have exactly one line?

Unfortunately not. file1 and file2 have a random number of lines. 

How to modify the code in order to create the output file? 
File1

1Linux aaa aaaa
2Solaris bb bbb
3Cygwin cccc cc
4Ubuntu dd dddd
5Fedora ee eeee
6Aix fff fff
7Hpux gggg gg

File2

We were born, in the earth1, beyond the land, Paris11
I love you, ich liebe dich2, ich mag dich, 22London
I am hungry3, ich habe durst, I am foolish, Berlin33

Output file

1Linux aaa aaaa
2Solaris bb bbb
We were born
in the earth1
beyond the land
Paris11
3Cygwin cccc cc
4Ubuntu dd dddd
I love you
ich liebe dich2
ich mag dich
22London
5Fedora ee eeee
6Aix fff fff
I am hungry3
ich habe durst
I am foolish
Berlin33
7Hpux gggg gg
3 added 1345 characters in body
source | link
2 Improve formatting, remove thanks
source | link
1
source | link