0

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.

J.S. Orris
  • 4,653
  • 12
  • 49
  • 89
  • 1
    `substr` in lowercase? – fedorqui Apr 20 '13 at 09:03
  • Yes, I need to throw a toLower in there as well – J.S. Orris Apr 20 '13 at 09:05
  • Yes, but I mean that the function is `substr`, not `Substr` (as far as I am concerned). – fedorqui Apr 20 '13 at 09:05
  • is that why its not working? – J.S. Orris Apr 20 '13 at 09:06
  • Check it! Could be one reason – fedorqui Apr 20 '13 at 09:07
  • Although the expressions I usually use are kind of `printf ("hello %s bye", tolower($1))`, not in between the strings. – fedorqui Apr 20 '13 at 09:09
  • get following stErr: /bin/nawk: syntax error at source line 17 context is print " " >>> ' <<< substr($1,1,1)}' "" ###Change this /bin/nawk: illegal statement at source line 17 /bin/nawk: extra } at source line 20 extra } – J.S. Orris Apr 20 '13 at 09:11
  • Im actually trying to pull the first letter of the first name and add it to the beginning of the last name to create the username. I need to do the tolower as well; can do to lower. Just trying to figure how to put my statement in line 17 for the user name. Am I missing brackets or something? this is the first time trying to append text to html using nawk – J.S. Orris Apr 20 '13 at 09:14
  • what does this stErr mean: context is print " " >>> { <<< 'substr($1,1,1)}' "" – J.S. Orris Apr 20 '13 at 09:17

1 Answers1

1

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>"
suspectus
  • 16,548
  • 8
  • 49
  • 57
  • Gosh, Im an idiot. Ive been hitting shift + insert with the wrong statement in my copy bin the whole time. Im an idiot (not really, just burnt out) been writing this and SQL statement for past 10 hours. LOL. Thank you for your help. I figured it out. – J.S. Orris Apr 20 '13 at 09:46
  • One thing though, Ive unset noclobber and when I run the following statemetn: mk_html.awk sample.txt > mysample.html It keeps saying mysample.html arlready exists in the stError. Why is this? – J.S. Orris Apr 20 '13 at 09:48
  • 1
    turn off noclobber with `set +o noclobber`. Or better use `>|` e.g. `mk_html.awk sample.txt >| mysample.html` to force overwrite. – suspectus Apr 20 '13 at 10:31