Search code examples
unixnawk

Trying to get first letter from first name in nawk script to html


Why is line 17 not working (commented in code)?:

#!/bin/nawk -f

BEGIN {
    print "<html>"
    print "<body>"
    print "  <table border=2>"
    print "    <tr>"
    print "      <th>Name</th>"              
    print "      <th>Username</th>"              
    print "      <th>Email</th>"                    
    print "    </tr>"
}

{
    print "    <tr>"
    print "      <td>" $2 " " $1"</td>"                   
    print "      <td>"'{Substr($1,1,1)}' "</td>"  ###### Line 17                 
is
    print "      <td>" $3 "</td>"             

Am I allowed to put a statement like that in line 17? Im trying to get the first letter of the first name.


Solution

  • The single quotes in this line should be removed. Currently the quoting allows the shell to parse Substr and you do not want that to happen.

    Also the command is substr not Substr.

    print "      <td>"'{Substr($1,1,1)}' "</td>"  ###### Line 17      
    

    change to-:

    print "      <td>" substr($1,1,1) "</td>"