With GNU awk
(i.e. gawk
) you could do:
gawk -i inplace '/mock/ { getline < "mock.txt" } 1' test{1..3}.txt
Like sed
, gawk
offers inplace editing. The above command looks for the regex-pattern mock
, and when it finds it, it replaces it with the next line from mock.txt
. The 1
is a pattern that always matches, and thus causes gawk
to perform its default action, which is to print the (newly) read line. Note that you won't actually see this line during inplace-editing; the print-action is necessary for the output to be recorded.