Error Messages


The following examples show some of the more common causes of awk error messages (and nonmessages). These examples are run under bash. When you use awk 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 awk four arguments instead of two.

$ awk {print $3, $1} cars awk: syntax error at source line 1  context is          >>>  <<< awk: illegal statement at source line 1         missing }


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

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


The next example has no braces around the action:

$ awk '/chevy/ print $3, $1' cars awk: syntax error at source line 1  context is         /chevy/ >>>  print <<<  $3, $1 awk: bailing out at source line 1


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

$ awk '/z/' cars


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

$ awk '{$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, awk sees two separate statements; the second is invalid.

$ cat print_cars BEGIN   {print "Model  Year    Price"} /chevy/ {printf "%5s\t%4d\t%5d\n", $2, $3, $5} $ awk -f print_cars cars awk: illegal statement  source line number 2


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

$ cat print_cars2 BEGIN {OFS='\t'} $3 ~ /5$/ {print $3, $1, "$" $5} $ awk -f print_cars2 cars awk: syntax error at source line 1 source file print_cars2  context is         BEGIN >>> {OFS=' <<< awk: illegal statement at source line 1 source file print_cars2





A Practical Guide to UNIX[r] for Mac OS[r] X Users
A Practical Guide to UNIX for Mac OS X Users
ISBN: 0131863339
EAN: 2147483647
Year: 2005
Pages: 234

Similar book on Amazon

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