script to insert variable file into another, bit like C's #include

Hi, I want to insert one file into another like this:

file 1:
1
2
3
<!-- #include file="file2" -->
4
5
<!-- #include file="file3" -->
6

with the complete output going to another file. Have seen some solutions to similar problems using sed, but this problem is different because it is unknown at the command line which files will need to be included.

Can this be done with sed or is there something better?

Cheers

You can "source" the files in your script like:

1
2
3
. ./file2
4
5
. ./file3
6

But I'm not sure if this is what you mean.

If the goal is to parse one "template" file into an output file, I've occasionally used PHP for that. The way it restricts itself to running code only inside PHP tags is very helpful, and you can make it as simple or as complicated as you want.

a
b
c
d
e
f
<?php readfile("filename"); ?>
g
h
i
j
k
l
php < template > output

Thank you, yes I am trying to do what Corona688 describes.

I was hoping to keep the <!-- #include file="file2" --> syntax if possible, because I am writing a website using Server Side Includes and this is the syntax for that, but I'm developing the site on a local machine (which won't do SSI) so want to mimic the effect with a script.

If I can't keep the SSI syntax I will try the php thing. Would then need to replace a large number of include lines before uploading to the server... is that any easier than the original problem? Hmmm.

Why not just install a webserver on your machine? Then you'll be able to use the language you want where you need it.

I don't think server side includes are that great a thing to base a site on though. My website was written in them, but I had to abandon them due to finicky incompatibilities and configuration changes every time I updated apache even slightly. Now that I've written it in PHP it's stayed stable.

Well I've written a little C++ program to do the includes - bit OTT but I understand that better than most scripts.

Did that before seeing your last post Corona688 - bit of a worry you found SSI so inconsistent. Will have to have a think about that.

Thanks for your help.