163 lines
4.4 KiB
Groff
163 lines
4.4 KiB
Groff
.TH validate "" "" "/rdb Command"
|
|
.PC "Find invalid data"
|
|
\fBvalidate '\fIpattern \fB{ \fIaction \fB} ...' < \fItableorlist\fR
|
|
.PP
|
|
.II "table^validate"
|
|
.II "data^validate"
|
|
The command
|
|
.B validate
|
|
checks your tables and a list of invalid data.
|
|
It also prints messages of your choosing, and shows
|
|
you the line that caused the error.
|
|
It is almost identical to a combination of the commands
|
|
.B row
|
|
and
|
|
.BR compute .
|
|
.PP
|
|
.B validate
|
|
passes your commands to
|
|
.B awk
|
|
after it has converted your column names to
|
|
.BR $1 ,
|
|
.BR $2 ,
|
|
and so on, which is how
|
|
.B awk
|
|
refers to columns.
|
|
.PP
|
|
.I pattern
|
|
is a logical condition that indicates invalidity in the data;
|
|
for example, it may give values that are negative and should not be,
|
|
or greater than they should be.
|
|
.I action
|
|
is what to do about it;
|
|
usually, this is to print a message.
|
|
.SH Example
|
|
The first example is simple.
|
|
Once again, consider table
|
|
.BR inventory ,
|
|
which is as follows:
|
|
.DM
|
|
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
|
|
6 89 147 13083 bunsen burners
|
|
7 5 175 875 scales
|
|
.DE
|
|
.PP
|
|
Several invalid values have crept into this table.
|
|
You might wish to check that no item in column
|
|
.B Onhand
|
|
is less than zero.
|
|
To do so, type the command:
|
|
.DM
|
|
validate 'Onhand < 0 {print "negative Onhand in line " NR}' < badinventory
|
|
.DE
|
|
.PP
|
|
This prints the following on the standard output:
|
|
.DM
|
|
Item# Onhand Cost Value Description
|
|
----- ------ ---- ----- --------------
|
|
1 3 50 150 rubber gloves
|
|
2 100 X 500 test tubes
|
|
negative Onhand in line 3
|
|
3 -5 80 -400 clamps
|
|
4 23 19 437 plates
|
|
negative Onhand in line 5
|
|
5 -99 24 -2376 cleaning cloth
|
|
6 89 147 13083 bunsen burners
|
|
7 5 175 875 scales
|
|
.DE
|
|
.PP
|
|
The variable
|
|
.B NR
|
|
means number of record or row.
|
|
This variable is built into
|
|
.BR awk ,
|
|
which
|
|
replaces it with the number of the line it is processing.
|
|
.PP
|
|
The next example is complex.
|
|
Note that if you wish, you can write a command line for
|
|
.B validate
|
|
into a file, to create a customized validation command.
|
|
You can put many tests and messages in one validation file.
|
|
For example, the following command file, called
|
|
.BR validateinv ,
|
|
thoroughly checks table inventory:
|
|
.DM
|
|
echo validating badinventory
|
|
.sp \n(pDu
|
|
validate '{if (Onhand < 0 ) print "negative Onhand in line " NR; \e
|
|
if (Value < 0 ) print "negative Value in line " NR; \e
|
|
if (Cost ~ /[A-Za-z]/) print "letter in Cost in line " NR; }' \e
|
|
< badinventory
|
|
.DE
|
|
.PP
|
|
To validate table
|
|
.BR inventory ,
|
|
just type:
|
|
.DM
|
|
validating badinventory
|
|
.DE
|
|
.PP
|
|
This prints the following on the standard output:
|
|
.DM
|
|
Item# Onhand Cost Value Description
|
|
----- ------ ---- ----- --------------
|
|
1 3 50 150 rubber gloves
|
|
letter in Cost in line 2
|
|
2 100 X 500 test tubes
|
|
negative Onhand in line 3
|
|
negative Value in line 3
|
|
3 -5 80 -400 clamps
|
|
4 23 19 437 plates
|
|
negative Onhand in line 5
|
|
negative Value in line 5
|
|
5 -99 24 -2376 cleaning cloth
|
|
6 89 147 13083 bunsen burners
|
|
7 5 175 875 scales
|
|
.DE
|
|
.PP
|
|
You may wish to redirect the output of
|
|
.B validate
|
|
to a file, bring it into your editor, and
|
|
correct the errors and remove the error lines as you go.
|
|
To make it easier to find those error lines in
|
|
the editor, you could have all error lines contain a string
|
|
that is easy to find, such as
|
|
.BR ERROR .
|
|
.PP
|
|
If you only want to see the error lines, use the command
|
|
.BR row .
|
|
.SH "Case of Column Names"
|
|
.I "(Ruby re-creation only.)"
|
|
Column names are case sensitive, as in the original
|
|
.BR /rdb .
|
|
If the environment variable
|
|
.B RDB_ICASE
|
|
is set to 1, upper and lower case are ignored when column names are looked up.
|
|
An exact match always wins;
|
|
if several columns differ only in case, the command stops with an
|
|
.I ambiguous
|
|
error.
|
|
Names that are also
|
|
.B awk
|
|
reserved words or are followed by
|
|
.B (
|
|
are never taken for columns.
|
|
.SH "See Also"
|
|
.SH "Logical Conditions"
|
|
.B
|
|
compute,
|
|
row
|
|
.R
|
|
.br
|
|
COHERENT Tutorials:
|
|
.B
|
|
Introduction to the awk Language
|
|
.R
|