1

Requirement: To generate a HTML page with CSS integration through shell script - awk

CSS: (mystyles.css)

Shell script command: (generate_html.sh)

awk 'BEGIN {print "<head>"}
    {print "<link rel="stylesheet" type="text/css" href="mystyles.css" media="screen" />"}
    {print "</head>"}
    {print "<table border="1" style=width:40% solid black >"}
END {print "</table>"}
{print "<tr bgcolor=#bfff80>\n<td>"NR, $0"</td>\n</tr>"}' server_list.txt > server_list.html

Exception: Syntax error for '.' of mystyles.css

awk: cmd. line:1:{print "<link rel="stylesheet" type="text/css" href="mystyles.css" media="screen" />"} 
awk: cmd. line:1:                                                             ^ syntax error

Please help me to resolve this issue.

1
  • You need to escape the double quotes while you try to print them Commented Sep 29, 2017 at 8:49

1 Answer 1

1
awk: cmd. line:1:{print "<link rel="stylesheet" type="text/css" href="mystyles.css" media="screen" />"} 
awk: cmd. line:1:                                                             ^ syntax error

Solution : You have to escape quotes

It seems your existing code, will put link, head, table and tr tag for each record read by awk, which does not produce html table correctly :

awk 'BEGIN {print "<head>"}
    {print "<link rel=\"stylesheet\" type=\"text/css\" href=\"mystyles.css\" media=\"screen\" />"}
    {print "</head>"}
    {print "<table border=\"1\" style=\"width:40% solid black\" >"}
    END {print "</table>"}
    {print "<tr bgcolor=\"#bfff80\">\n<td>"NR, $0"</td>\n</tr>"}' server_list.txt

Probably I guess you need below one:

awk '
BEGIN{
        # meta header and loading css
        print "<head>"
        print "<link rel=\"stylesheet\" type=\"text/css\" href=\"mystyles.css\" media=\"screen\" />"
        print "</head>"

        # start table
        print "<table border=\"1\" style=\"width:40% solid black\" >"
    }
        # print row and cell
    {
        print "<tr bgcolor=\"#bfff80\">\n<td>"NR, $0"</td>\n</tr>"
    }
END {
        # close table tag
        print "</table>"
    }
    ' server_list.txt

For example with test file

$ cat testfile 
row1
row2
row3
row4

It will output :

<head>
<link rel="stylesheet" type="text/css" href="mystyles.css" media="screen" />
</head>
<table border="1" style="width:40% solid black" >
<tr bgcolor="#bfff80">
<td>1 row1</td>
</tr>
<tr bgcolor="#bfff80">
<td>2 row2</td>
</tr>
<tr bgcolor="#bfff80">
<td>3 row3</td>
</tr>
<tr bgcolor="#bfff80">
<td>4 row4</td>
</tr>
</table>

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks @Akshay for the rectification. html file got created properly as required. However My requirement is to embed the css stylesheet to html. which is not reflecting properly. below is my css file content. Can you pls help me here..... .circle-text { display: table-cell; height: 200px; /*change this and the width for the size of your initial circle*/ width: 200px; text-align: center; vertical-align: middle; border-radius: 50%; /*make it pretty*/ background: #555; color: #fff; font: 18px "josefin sans", arial; /*change this for font-size and font-family*/ }
Hi @MarcLambrichs, I was trying to embed the css file to the html file to be generated through awk command. please find my css file used. referr to the following link. gist.github.com/hmsvigle/a7fbe88de3ff6eac318ac021f5d8d539
@Himansu you have not used class attribute, please use class="circle-text"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.