Let's say I have a file, Foo.in
:
Contents of Foo
and Bar.in
INSERT Foo.in
Contents of Bar
I would like to write a sed
script that replaces INSERT xyz
with actual contents of the xyz
file. In this particular example I'd like to produce Bar.out
file containing:
Contents of Foo
Contents of Bar
I thought about using sed
's r
command as demonstrated in this Stack Overflow question, but the problem is that the name of the file to be inserted is specified in the file itself. I thought about scanning the file for INSERT
commands and then running sed
separately for every INSERT
found, but that is a horrible solution with O(n^2) complexity. I'd prefer to do this using sed or AWK, but if all else fails a Perl script would be acceptable.