Issue
I'm trying to generate a html file from a text file called index.db. Contents of index.db:
file="test.html"
date="2013-01-07"
title="Example title"
file="test2.html"
date="2014-02-04"
title="Second example title"
The command I'm trying:
sed '/^$/d;H;1h;$!d;g;s/\n\t\+/ /g' input/index.db |
while read -r line; do
awk '{
print "<h1>"title"</h1>"
print "<b>"date"</b>"
print "<a href=\""file"\">"file"</a>"
}' $line
done
And it returns:
awk: fatal: cannot open file `title"' for reading: No such file or directory
awk: fatal: cannot open file `example' for reading: No such file or directory
But if I try the following command, it runs perfectly:
sed '/^$/d;H;1h;$!d;g;s/\n\t\+/ /g' input/index.db |
while read -r line; do
echo $line
awk '{
print "<h1>"title"</h1>"
print "<b>"date"</b>"
print "<a href=\""file"\">"file"</a>"
}' file="test.html" date="2013-01-07" title="Example title"
done
Solution
with some reusable function to wrap html tags.
$ awk -F'[="]' -v RS= -v OFS='\n' -v ORS='\n\n' '
function h(t,r,v) {return "<" t (r?" href=\"" r "\"":"") ">"v "</"t">"}
{print h("h1","",$9), h("b","",$6), h("a",$3,$3)}' file
<h1>Example title</h1>
<b>2013-01-07</b>
<a href="test.html">test.html</a>
<h1>Second example title</h1>
<b>2014-02-04</b>
<a href="test2.html">test2.html</a>
Answered By - karakfa