Replace data at specific positions in txt file using data from another file

awkpastesedtext processing

I have a text file in the below format:

$data This is the experimental data    
good data
This is good file
datafile
1 4324 3673 6.2e+11 7687 67576
2 3565 8768 8760 5780 8778          "This is line '2'"
3 7656 8793 -3e+11 7099 79909
4 8768 8965 8769 9879 0970
5 5878 9879 7.970e-1 9070 0709799
.
.
.
100000 3655 6868 97879 96879 69899
$.endfile

I want to replace the data of the 3rd and 4th column from row '2' to '100000' with the data from two other text files which have one column of 99999 rows each.

How can I do this using awk, sed or any other unix command?
Note that the column delimiter is space.

The other two text files have 99999 lines each, and they are both in the following format:

12414
12421
36347
3.4e+3
-3.5e22
987983
.
.
.
87698

Best Answer

Since you haven’t asked for a 100% awk solution, I’ll offer a hybrid that (a) may, arguably, be easier to understand, and (b) doesn’t stress awk’s memory limits:

awk '
    $1 == 2 { secondpart = 1 }
       { if (!secondpart) {
                print > "top"
         } else {
                print $1, $2 > "left"
                print $5, $6, $7, $8, $9 > "right"
         }
       }' a
(cat top; paste -d" " left b c right) > new_a
rm top left right

Or we can eliminate one of the temporary files and shorten the script by one command:

(awk '
    $1 == 2 { secondpart = 1 }
       { if (!secondpart) {
                print
         } else {
                print $1, $2 > "left"
                print $5, $6, $7, $8, $9 > "right"
         }
       }' a; paste -d" " left b c right) > new_a
rm left right

This will put some extra spaces at the ends of the lines of the output, and it will lose data from file a if any line has more than nine fields (columns).  If those are issues, they can be fixed fairly easily.

Related Question