-1

say i have file1 with content

line1
line2
line3

and another file2 with content

lineA
lineB
lineC
lineD
lineE
lineF
lineG
lineH
lineI

I want to make file2 as

lineA
lineB
lineC
line1
lineD
lineE
lineF
line2
lineG
lineH
lineI
line3
JNevill
  • 46,980
  • 4
  • 38
  • 63
psi
  • 77
  • 1
  • 11
  • How big are these two files? Is it just a few dozen records, thousands, millions? – JNevill Dec 14 '17 at 20:15
  • Are you able to use python or some more advanced language instead? – TCulp Dec 14 '17 at 20:18
  • there are a few dozen records, and yes i can use python – psi Dec 14 '17 at 20:19
  • Oops, you forgot to post your code! StackOverflow is about helping people fix their code. It's not a free coding service. Any code is better than no code at all. Meta-code, even, will demonstrate how you're thinking a program should work, even if you don't know how to write it. – ghoti Dec 14 '17 at 22:52
  • @ghoti I had no code, not even a meta-code to post. StackOverflow is about helping people, that's what I needed HELP. And those who wanted to help, they did. Anyway I will keep your suggestion(which is not HELP), in mind for future. Thanks a lot. – psi Dec 15 '17 at 11:16
  • @mnu - StackOverflow is about helping people learn. It's not intended as a mechanism to get handouts, though it is unfortunately used for that all too often. Since your question is tagged **bash** and **csh**, I would expect to see bash code and csh code in your question. Check out [how-to-ask](http://stackoverflow.com/help/how-to-ask) for tips on what makes a good question. – ghoti Dec 15 '17 at 11:23

4 Answers4

1

Here is a way to do it with paste

cat file2 | paste -d'\n' - - - file1

The dash argument for paste means to read from the standard input, which is the cat file2 output, while the fourth argument is file1. So, with three dashes, we will paste every 3 lines of one file with 1 from another and the delimiter is the newline character (-d'\n').

This will work in case of remaining lines in any of these files, as paste will continue when EOF is found for one of the inputs. But it may print a couple of empty lines in that case, so you can pipe to any command to remove them, (supposing you don't have actual empty lines in your files), for example

cat file2 | paste -d'\n' - - - file1 | sed '/^$/d'
thanasisp
  • 5,855
  • 3
  • 14
  • 31
0

This python code will do it, the parameters in your case would be

python interlace.py file1 file2 file3 3

I would suggest just using a mv file3 file2 afterward if you want it to be in-place. This is because if you start writing to file2 before you've read everything it can be overwritten

import sys

if len(sys.argv[1:]) == 4:
    file1 = open(sys.argv[1], 'r')
    file2 = open(sys.argv[2], 'r')
    file3 = open(sys.argv[3], 'w')
    line_count = int(sys.argv[4])

    current_counter = 0
    for file2_line in file2.readlines():
        current_counter += 1
        file3.write(file2_line)
        if current_counter == line_count:
            file3.write(file1.readline())
            current_counter = 0

    for file1_line in file1.readlines():
        file3.write(file1_line)

    file3.close()

This also works in the cases where file1 runs out of lines early, in which case file2's lines continue as normal, and when file1 has extra lines they just get added to the end.

TCulp
  • 332
  • 2
  • 9
0

This might work for you (GNU sed):

n=3
sed "$n~$n"'R file1' file2

After the third line and subsequently every third line of file2, append a line from file1.

potong
  • 55,640
  • 6
  • 51
  • 83
  • This doesn't add any remaining lines from file1 if file2 runs out of lines, though the question didn't specify intended behavior in that case – TCulp Dec 14 '17 at 20:47
0

Using awk and getline:

awk '1;NR%3==0{if((getline < "file1")>0)print}' file2
lineA
lineB
lineC
line1
lineD
...

You could probably obfuscate it to awk '1;NR%3==0&&(getline < "file1")' file2 (untested).

James Brown
  • 36,089
  • 7
  • 43
  • 59