Error Messages

 < Day Day Up > 

The following examples show some of the more common causes of gawk error messages (and nonmessages). These examples are run under bash. When you use gawk with tcsh, the error messages from the shell will be different.

The first example leaves the single quotation marks off the command line, so the shell interprets $3 and $1 as shell variables. Also, because there are no single quotation marks, the shell passes gawk four arguments instead of two.

 $ gawk {print $3, $1} cars gawk: cmd. line:2: (END OF FILE) gawk: cmd. line:2: syntax error 

The next command line includes a typo (prinnt) that gawk does not catch. Instead of issuing an error message, gawk simply does not do anything useful.

 $ gawk '$3 >= 83 {prinnt $1}' cars 

The next example has no braces around the action:

 $ gawk '/chevy/ print $3, $1' cars gawk: cmd. line:1: /chevy/ print $3, $1 gawk: cmd. line:1:         ^ syntax error 

There is no problem with the next example; gawk did just what you asked it to do (none of the lines in the file contains a z).

 $ gawk '/z/' cars 

The next example shows an improper action for which gawk does not issue an error message:

 $ gawk '{$3  " made by "  $1}' cars 

The following example does not display the heading because there is no backslash after the print command in the BEGIN block. The backslash is needed to quote the following NEWLINE so that the line can be continued. Without it, gawk sees two separate statements; the second does nothing.

 $ cat print_cars BEGIN   {print "Model  Year    Price"} /chevy/ {printf "%5s\t%4d\t%5d\n", $2, $3, $5} $ gawk -f print_cars cars malibu  1999     3000 malibu  2000     3500 impala  1985     1550 

You must use double quotation marks, not single ones, to delimit strings.

 $ cat print_cars2 BEGIN {OFS='\t'} $3 ~ /5$/  {print $3, $1, "$" $5} $ gawk -f print_cars2 cars gawk: print_cars2:1: BEGIN {OFS='\t'} gawk: print_cars2:1:            ^ invalid char ''' in expression 

     < Day Day Up > 


    A Practical Guide to LinuxR Commands, Editors, and Shell Programming
    A Practical Guide to LinuxR Commands, Editors, and Shell Programming
    ISBN: 131478230
    EAN: N/A
    Year: 2005
    Pages: 213

    flylib.com © 2008-2017.
    If you may any questions please contact us: flylib@qtcs.net