1

With input files A and B as such:

File A

1 1 1
2 2 2
3 3 3

File B

10 10 10
20 20 20
30 30 30
40 40 40
50 50 50
60 60 60 
70 70 70

I would like to insert one line of file A every 3 lines of file B, starting with the first line being the first line of file A so that the output would like so:

1 1 1
10 10 10
20 20 20
30 30 30
2 2 2
40 40 40
50 50 50
60 60 60 
3 3 3 
70 70 70

My original attempt at this involved creating separate files for every three lines, but my dataset is extremely large so I'm interested in saving hard drive space.

  • Could you please post what you tried? – Sparhawk Mar 26 '18 at 23:56
2

If you don't need to use awk, paste could work:

$ paste -d '\n' <fileB fileA - - - | tr -s '\n' >fileC

The tr part isn't strictly necessary, so you can remove it if you don't mind extra newlines.

1

Sounds like a good case for getline:

awk '(NR-1)%3 == 0 {getline line < "FileA"; print line} 1' FileB
1 1 1
10 10 10
20 20 20
30 30 30
2 2 2
40 40 40
50 50 50
60 60 60 
3 3 3
70 70 70
0

With GNU sed’s R command:

sed -e $'R fileb\nR fileb\nR fileb' filea > output

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.