A fuller understanding of compute and row can be obtained from the UNIX awk tutorial and The AWK Programming Language, by Aho, Kernighan and Weinberger.
$ cat inventory Item Amount Cost Description ---- ------ ---- ----------- 1 4 50 rubber gloves 2 100 5 test tubes 3 5 80 clamps 4 23 19 plates 5 99 24 cleaning cloth 6 89 147 bunsen burnersEXPRESSIONSaddcol Width Value < inventory |\\ compute 'Cost = sprintf ("%.2f", Cost) ; Value = Cost * Amount ; Width = length(Description) ; if (Width > 11) Description = sprintf("%.11s", Description)'
Item Amount Cost Description Width Value ---- ------ ------ ----------- ----- ----- 1 4 50.00 rubber glov 13 200 2 100 5.00 test tubes 10 500 3 5 80.00 clamps 6 400 4 23 19.00 plates 6 437 5 99 24.00 cleaning cl 14 2376 6 89 147.00 bunsen burn 14 13083
if then else:
if (expression) equation [ else equation ];
if (Value > 10000) Status = "Special"; else Status = "Normal";
while loop: while (expression) equation;
while (column => 0) column -= 1 ;
for loop: for (expression; condition; expression) equation;
for (i = 0; i < column1; i++) array [i] = column2 + i ;
Regular awk syntax, "pattern { action }" can also be applied, for example:
$ compute 'length > 80 { print NR, "Line too long." }' < table
awk recognizes length as a function that returns the length of the row and NR as the line number. Note that the line numbers of a file starts with the head line, while row numbers of a table starts after the dash line (line 3). COLUMN NAMES
compute defines a column name to be a string of upper and lower letters, numbers, underscore (_) and any set of characters enclosed in single or double quotes. It is important to enclose column names in quotes if they contain metacharacters, ie.:
$ compute ' "Item#" = NR ' < inventory
Note the double quotes around the column head Item# to protect interpretation of `#'. In general, if one kind of quote is used around the whole program, the other kind of quote should be used around column names that contain special characters. The exception(s) to this rule are discussed in the section on using quotes and shell variables. RESERVED AWK WORDS
$ DATE=920905 $ compute 'Date = '$DATE'' < journal Date Account Debit Credit Description ------ ------- ----- ------ -------------------------- 920905 101 25000 cash from loan 920905 211.1 25000 loan number #378-14 920905 150.1 10000 Zarkoff test equipment 920905 101 5000 cash payment 920905 211.2 5000 note payable to Zarkoff 920905 130 30000 inventory parts / CCPSC 920905 201.1 15000 accounts payable / CCPSC 920905 101 15000 payment to CCPSC for parts
The innermost quotes surrounding $DATE act as a special `window' into which shell interpretation can occur. The example works because the value of $DATE is numeric. Passing string variables to compute requires that they be enclosed in double quotes:
$ COMPANY=Makeapile $ compute 'Company = "'$COMPANY'"' < maillistNumber 1 Name Ronald McDonald Company Makeapile Street 123 Mac Attack City Memphis State TENN ZIP 30000 Phone (111) 222-3333
Number 2 Name Chiquita Banana Company Makeapile Street Uno Avenito De La Revolution City San Jose State El Salvadore ZIP 123456789 Phone 1234
Here a shell variable named COMPANY is set to a string. In the compute command, the outermost set of quotes (') protect the shell from interpreting anything within them (with the exception of other pairs of 's); the next set of quotes (") indicates that the variable being set should be treated as a string; and the innermost set (') provide the special window for the shell to perform variable substitution. The command which compute gets, after the shell is finished with it, is
$ compute 'Company = "Makeapile"' < maillist
The example above worked okay because the shell variable we set happened to be only one word. The special window technique does not work for variables which contain blanks: passing multi-word shell variables to compute requires that both the statement and the variable be enclosed in double quotes. Unlike single quotes, the outermost set of double quotes allow for shell expansion of anything which they enclose; the innermost set of quotes are protected (escaped) so that the shell will pass them through to compute:
$ COMPANY="Revolutionary Software, Inc." $ compute "Company = \\"$COMPANY\\"" < maillistSEE ALSONumber 1 Name Ronald McDonald Company Revolutionary Software, Inc. Street 123 Mac Attack City Memphis State TENN ZIP 30000 Phone (111) 222-3333
Number 2 Name Chiquita Banana Company Revolutionary Software, Inc. Street Uno Avenito De La Revolution City San Jose State El Salvadore ZIP 123456789 Phone 1234