The specification does not cover every details, so I have the following assumptions.
- The
<!--#include file="myfile.html"-->
line stays its own. Other characters are removed. - Included files does not contains additional includes.
- In project directory no subdirs has to be checked.
In this case something like this can do the job. It is in bash:
#!/usr/bin/bash
search=${1:-./}
replace() {
while read -r x; do
if [[ "$x" =~ \<!--#include\ file=\"([^\.]+.html)\"--\> ]]; then
cat "${BASH_REMATCH[1]}";
else
echo "$x"
fi
done <"$1"
}
while read f; do
replace "$f" > tmp_$$.tmp && mv tmp_$$.tmp "$f"
done < <(find $search -maxdepth 1 -name '*.html')
It reads all the *.html
files in the specified directory (not recursively). If no args given it checks the current directory. For each line it calls replace
function. Replace
searches for include lines. If one found, then prints the content of the file to the stdout, otherwise the original line is presented.
Lets consider to files:
cat >master.html <<XXX
<html>
<!--#include file="myfile.html"-->
</html>
XXX
cat >myfile.html <<XXX
<title>
My file
</title>
XXX
Result:
$ cat master.html
<html>
<title>
My file
</title>
</html>
$ cat myfile.html
<title>
My file
</title>
I hope this could help...