0

I am working with a netCDF file that has a lot of variables and I need to extract only those that start with the string Var_. The only answers that I found that came close to what I am asking were done by using ncdump however I need to do that with ncks.

I tried the following command but it ended up extracting every single variables that were in the input file instead of the ones starting with Var_ only.

ncks -C -v time,lon,lat,Var_* input.nc output.nc #(I also need the time, lon and lat variables)

I tried to do it that way because the numbers of variables in the input file can increase/decrease which is why I can't write the variables names one by one in my script.

Is there a way to do that? Also, this is for a bash script that I am writing.

I.M.
  • 344
  • 3
  • 14

1 Answers1

2

Your command would have worked had you used regular expression (RX) syntax (documentation) rather than shell-globbing syntax in the variable list. NCO need RX syntax for variable/attribute/dimension lists contained in files, and only uses shell-globbing for filenames/paths that the shell sees. Re-try with something like

ncks -C -v time,lon,lat,^Var_.? input.nc output.nc
Charlie Zender
  • 5,929
  • 14
  • 19
  • Thank you for explaining what I did wrong! I'm not used to work with both nco modules and bash so I didn't know that there were different synthax. – I.M. Jun 01 '23 at 09:03