Assume a multi-line text file file1
, where some lines contain the keyword "keyw".
$ cat file1
foo
bar keyw
baz
keyw qux
quux
Further assume a single-line text file file2
that contains as many strings as keyword occurrences in file1
. The strings in file2
are separated by single whitespaces.
$ cat file2
string1 string2
I would like to append each string of file2
to a keyword-containing line of file1
based on the respective positions:
The first string in
file2
appended to the first line infile1
that contains the keyword.The second string in
file2
appended to the second line infile1
that contains the keyword.etc.
Here is the sought output:
$ awk ... file1 file2
foo
bar keyw string1
baz
keyw qux string2
quux
What awk-code would you use to conduct this replacement?