Skip Navigation Links 
NOAA logo - Click to go to the NOAA home page National Weather Service   NWS logo - Click to go to the NWS home page
Climate Prediction Center

About Us
   Our Mission
   Who We Are

Contact Us
   CPC Information
   CPC Web Team

HOME > Monitoring_and_Data > Oceanic and Atmospheric Data > Reanalysis: Atmospheric Data > wgrib2-grep (various)

wgrib2: -egrep, -egrep_v, -fgrep, -fgrep_v


When you use wgrib2 extensively, common sequences keep occuring, such as,

   1:  wgrib2 A.grb >A.inv
   2:  cat A.inv | fgrep ":HGT:" | fgrep ":500 mb:" | wgrib2 -i A.grb -grib hgt500.grb
   3:  cat A.inv | fgrep ":TMP:" | fgrep ":500 mb:" | wgrib2 -i A.grb -grib tmp500.grb
   4:  cat A.inv | egrep ":(UGRD|VGRD):" | fgrep ":500 mb:" | wgrib2 -i A.grb -grib wind500.grb
Using the various -grep, -inv and the -i_file option, the above example can be written as
   1:  wgrib2 A.grb -inv A.inv
   2:  wgrib2 -fgrep ":HGT:" -fgrep ":500 mb:" -i_file A.inv A.grb -grib hgt500.grb
   3:  wgrib2 -fgrep ":TMP:" -fgrep ":500 mb:" -i_file A.inv A.grb -grib tmp500.grb
   4:  wgrib2 -egrep ":(UGRD|VGRD):" -fgrep ":500 mb:" -i_file A.inv A.grb -grib wind500.grb
The first version is easier to read. So why were the extra options added?
  1. Some shells have problems with pipes?
    • Some versions of Windows dos-prompt have problems with pipes.
    • RNomads: solved Windows 7 problem by using these options
  2. More efficient when you avoid multiple processes and pipes?
    • Every millisecond and K byte counts!
    • Will test with Nomads' grib_filter to see if number of zombies is reduced.
  3. Callable wgrib2?
    • A subroutine (wgrib2) can read a field using the index file!
The options were added for the third reason. I wanted to have a calls to wgrib2 that would read a selected field. The above example can be written in fortran as,
   include wgrib2api
   i = wgrib2a('A.grb','-inv','A.inv')
   i = wgrib2a('-fgrep',':HGT:','-fgrep',':500 mb:','-i_file,'A.inv','A.grb','-grib','hgt500.grb')
   i = wgrib2a('-fgrep',':TMP:','-fgrep',':500 mb:','-i_file','A.inv','A.grb','-grib','tmp500.grb')
   i = wgrib2a('-egrep',':(UGRD|VGRD):','-fgrep',':500','mb:','-i_file','A.inv','A.grb','-grib','wind500.grb')

The -grep options are used in wgrib2api's grb2_inq(..) function.

Definition of grep options:

    (...) | wgrib2 -OP1 X (...)
      behaves like
    (...) | OP2 X | wgrib2 (...) 

      if OP1 == egrep       then OP2 = egrep
      if OP1 == fgrep       then OP2 = fgrep
      if OP1 == egrep_v     then OP2 = egrep -v
      if OP1 == fgrep_v     then OP2 = fgrep -v

    X is a posix extended regular expression (egrep, egrep_v)
    or a fixed string (fgrep, fgrep_v)

   The number of -fgrep and -fgrep_v options is limited to 200.
   The number of -egrep and -egrep_v options is limited to 200.
   The wgrib2 option -set_regex does not affect the -grep options.


-egrep X
-egrep_v X
-fgrep Y
-fgrep_v Y

X is a posix extended regular expression
Y is a fixed string (not a regular expression)
Note: -set_regex does not modify the type of regex for these options

See also: -match, -not, -match_inv, -end, -if, -i_file, -not_if, -set_regex.

NOAA/ National Weather Service
National Centers for Environmental Prediction
Climate Prediction Center
5830 University Research Court
College Park, Maryland 20740
Climate Prediction Center Web Team
Page last modified: Feb 3, 2017
Disclaimer Privacy Policy