3.46 msel - Select Records with Conditions

Define the computation criteria at c= parameter, the record is selected if condition returns true. All operators and functions available in mcal command can be used in the conditional function. For more details, please refer to mcal.

Format

msel c= [u=] [-r] [i=] [o=] [-nfn] [-nfno] [-x] [--help] [--version]

Parameters

c=

Define the expression using combinations of operators and functions.

 

Refer to mcal for more details.

o=

Records matching the condition will be printed to this output file.

u=

Records that do not match the condition will be printed to this output file.

-r

Reverse selection

 

Select records excluded from the selection condition defined in c=

Examples

Example 1: Basic example

Select records where "Amount" is greater than 40. Write the unmatched records to a different output file file unmatch1.csv.

$ more dat1.csv
Customer,Quantity,Amount
A,1,10
A,2,20
B,1,30
B,3,40
B,1,50
$ msel c='${Amount}>40' u=unmatch1.csv i=dat1.csv o=match1.csv
#END# kgsel c=${Amount}>40 i=dat1.csv o=match1.csv u=unmatch1.csv
$ more match1.csv
Customer,Quantity,Amount
B,1,50
$ more unmatch1.csv
Customer,Quantity,Amount
A,1,10
A,2,20
B,1,30
B,3,40

Example 2: Selecting records with null value(s)

No records will be selected when the condition defined c= returned a null value. Records that do not match the condition will be written to a separate file defined in u=.

In the following example, the first three rows of data from column b are -1, null, and 1. When selecting records where b is greater than 0, the query record with a null value will be treated as an exception saved in the unmatched records file.

$ more dat2.csv
a,b
A,-1
B,
C,1
$ msel c='${b}>0' i=dat2.csv o=match2.csv u=unmatch2.csv
#END# kgsel c=${b}>0 i=dat2.csv o=match2.csv u=unmatch2.csv
$ more match2.csv
a,b
C,1
$ more unmatch2.csv
a,b
A,-1
B,

Example 3: Specify -r option

Null value is always evaluated as a unknown value regardless of the condition. Thus, records with null value is not selected.

In the following example, the reverse selection parameter -r is used with the same condition in the previous example. Even though the selection criteria is inverted, the query record with a null value will be treated as an exception saved in the unmatched records file as in the previous example.

$ msel -r c='${b}>0' i=dat2.csv o=match3.csv u=unmatch3.csv
#END# kgsel -r c=${b}>0 i=dat2.csv o=match3.csv u=unmatch3.csv
$ more match3.csv
a,b
A,-1
$ more unmatch3.csv
a,b
B,
C,1

Related Commands

mselnum : Select records with simple numeric range.

mselstr : Select records matching query string

mcal : Return the calculated results instead of selecting records.