Replace certain token with the content of a file (using a bash-script)

awk, bash, replace, sed

Solution

If you are okay with Perl you can do:

$ cat FILE1
this is file1

$ cat FILE2
this is file2

$ cat file
foo
INSERT_HERE1
bar
INSERT_HERE2
baz

$ perl -ne 's/^INSERT_HERE(\d+)\s+$/`cat FILE$1`/e;print' file
foo
this is file1
bar
this is file2
baz
$ 

Problem

I have a file containing some text and the words `INSERT_HERE1` and `INSERT_HERE2`. I'd like to replace these words with the content of `file1.txt` and `file2.txt` respectively. I suspect `sed` or `awk` could pull it off but I've basically never used them.

Original source