Search code examples
bashshellsedmergecat

Linux bash weave two text files into one alternative line using all available lines


I am looking to merge two text files into one, where all the lines from one text file become odd line numbers and the other file becomes even line numbers such as to weave the files together

input1.txt

1, 267, Note_on_c, 0, 67, 100
1, 758, Note_on_c, 0, 58, 100
1, 1248, Note_on_c, 0, 79, 100
1, 1739, Note_on_c, 0, 52, 100

input2.txt

1, 368, Note_off_c, 0, 67, 127
1, 936, Note_off_c, 0, 58, 127
1, 1415, Note_off_c, 0, 79, 127
1, 1917, Note_off_c, 0, 52, 127

and need to combine these text files to create the following output

1, 267, Note_on_c, 0, 67, 100
1, 368, Note_off_c, 0, 67, 127
1, 758, Note_on_c, 0, 58, 100
1, 936, Note_off_c, 0, 58, 127
1, 1248, Note_on_c, 0, 79, 100
1, 1415, Note_off_c, 0, 79, 127
1, 1739, Note_on_c, 0, 52, 100
1, 1917, Note_off_c, 0, 52, 127

so I am looking to merge these files in the true since of the word weave


Solution

  • With GNU sed's R command:

    sed 'R input2.txt' input1.txt
    

    Output:

    1, 267, Note_on_c, 0, 67, 100
    1, 368, Note_off_c, 0, 67, 127
    1, 758, Note_on_c, 0, 58, 100
    1, 936, Note_off_c, 0, 58, 127
    1, 1248, Note_on_c, 0, 79, 100
    1, 1415, Note_off_c, 0, 79, 127
    1, 1739, Note_on_c, 0, 52, 100
    1, 1917, Note_off_c, 0, 52, 127
    

    From man sed:

    R filename: Append a line read from filename. Each invocation of the command reads a line from the file. This is a GNU extension.