NOTE: the -set_regex option MUST preceed the -match/-not/-if/-not_if options. All these
options are "setup" options and the processing the the -set_regex must occur before the
processing of the -match and other regex options.
Grib files can have hundreds of records and most people only want a few of
the records. Rather than processing everything, you can select the
specific records to process by the -match,
-if and -not if options. These options
take an extended POSIX regular expression (regex) as their sole arguement. These
options check the regex with the "match inventory" (see -match_inv).
Some examples are
wgrib2 input.grb -match ':UGRD:200 mb:' -grib u.grb
wgrib2 input.grb -match ':(UGRD|VGRD|TMP):200 mb:' -grib uvt.grb
Now regex are powerful but can produce some surprises. For example, you want
the 19th (positive perturbation) ensemble member which is denoted by
'ENS=+19' in the match inventory. You try,
wgrib2 input.grb -match ':ENS=+19:' -grib e19.grb
Surprise, the above line does not work. The plus sign is a regex
metacharacter indicating that the previous character would be
matched 1 or more times. Consequently the plus sign wouldn't
be matched. To get the above match to work, you can quote the plus sign
with a backslash.
wgrib2 input.grb -match ':ENS=\+19:' -grib e19.grb
Alternatively you could change the regex match into "pattern" mode.
In pattern mode, the regex metacharacters are considered to be
wgrib2 -set_regex 1 input.grb -match ':ENS=+19:' -grib e19.grb
You can consider "-set_regex 0" which is the default to be the egrep mode and the
"set_regex 1" to be the grep mode. There is a third mode in which you need
to quote the metacharacters. Here is an example that gets the 19th, 20th and
21th ensemble members. This mode was added because it is easier to remember
to quote the '(|)' metacharacters than to quote the ordinary characters
correspond to metacharacters.
wgrib2 -set_regex 2 input.grb -match ':ENS=+\(19\|20\|21\):' -grib e19_20_21.grb
-set_regex X X=0, 1, 2
$ wgrib2 gep.grb -match ':UGRD:200 mb:' -match ':ENS=+19:'
$ wgrib2 gep.grb -set_regex 1 -match ':UGRD:200 mb:' -match ':ENS=+19:'
4.1:86046:d=2009060500:UGRD:200 mb:180 hour fcst:ENS=+19