I'm completely new to bash scripting so excuse me.... I am trying to combine some html content with a template that contains standard headings, the template has a place-holder "REPLACEME" which I thought I could just find and replace on. The loop simply repeats the operation on all the files in the directory.
REPLACEME="REPLACEME"
for file in *.html
do
TEMPLATE=$(<../template/template.html)
CONTENT=$(<$file)
OUTPUT="${TEMPLATE/"$REPLACEME"/"$CONTENT"}"
echo $OUTPUT > ../compiled/$file
done
This works but the resulting html file has been stripped of new line characters, which makes it look like junk! Can anyone help?
Replace:
With:
The shell performs word splitting on unquoted variables. With the default value for
IFS
, this means that all sequences of whitespace, which includes tabs and newlines, are replaced with a single blank. To prevent that, put the variable in double-quotes as shown above.Using
sed
you could achieve it like below :The
-i
option in sed is for inplace edit & theg
option is for global substitution.Edit 1:
If you need to use a variable inside
sed
you can do it this wayMind the double quotes here, it makes the shell expand variables.
If your system supports gnu-awk (
gawk
) you may achieve the above with