Pattern is a logical condition that would be invalid, such as values that are negative and should not be, or greater than they should be. Action is what to do about it. Usually the action is to print out a message.
$ cat inventory Item Onhand Cost Value Description ---- ------ ---- ----- -------------- 1 3 50 150 rubber gloves 2 100 X 500 test tubes 3 -5 80 -400 clamps 4 23 19 437 plates 5 -99 24 -2376 cleaning cloth
There are several invalid values in this table. To check that no Onhand items are less than zero:
$ cat inventory |\\
validate 'Onhand < 0 { print "negative Onhand line " NR }'
Item Onhand Cost Value Description
---- ------ ---- ----- --------------
1 3 50 150 rubber gloves
2 100 X 500 test tubes
negative Onhand line 5
3 -5 80 -400 clamps
4 23 19 437 plates
negative Onhand line 7
5 -99 24 -2376 cleaning cloth
5 -99 24 -2376 cleaning cloth
NR is a built-in awk variable which refers to the current line in the file. We could subtract 2 from NR to get the logical row number. A more complex example involves many tests and messages, executed as one validation file.
$ cat valfile
{if (Onhand < 0 ) print "negative Onhand in row " NR-2;
if (Value < 0 ) print "negative Value in row " NR-2;
if (Cost ~ /[A-Za-z]/) print "letter in Cost in row " NR-2}
$ validate -f valfile < inventory
Item Onhand Cost Value Description
---- ------ ---- ----- --------------
1 3 50 150 rubber gloves
letter in Cost in row 2
2 100 X 500 test tubes
negative Onhand in row 3
negative Value in row 3
3 -5 80 -400 clamps
4 23 19 437 plates
negative Onhand in row 5
negative Value in row 5
5 -99 24 -2376 cleaning cloth
SEE ALSO