使用bash提取vdate形式的nc标题,而无需打开文件

问题描述 投票:0回答:1

我必须通过将vdate从其标题添加到标题来更改数百个文件的标题。

如果vdate = 19971222,那么我希望该nc文件的名称成为rerun4_spindown_19971222.nc

我知道我可以通过ncdump -h filename找到vdate(请参见下面的示例标题)。

ncdump -h rerun4_1997_spindown_09191414_co2 
netcdf rerun4_1997_spindown_09191414_co2 {
dimensions:
    lon = 768 ;
    lat = 384 ;
    nhgl = 192 ;
    nlevp1 = 96 ;
    spc = 32896 ;

// global attributes:
        :file_type = "Restart history file" ;
        :source_type = "IEEE" ;
        :history = "" ;
        :user = " Linda" ;
        :created = " Date - 20190919 Time - 134447" ;
        :label_1 = " Atmospheric model " ;
        :label_2 = " Library 23-Feb-2012" ;
        :label_3 = " Lin & Rood ADVECTION is default" ;
        :label_4 = " Modified physics" ;
        :label_5 = " Modified radiation" ;
        :label_6 = " Date - 20190919 Time - 134447" ;
        :label_7 = " Linda " ;
        :label_8 = " Linux " ;
        :fdate = 19950110 ;
        :ftime = 0 ;
        :vdate = 19971222 ;
        :vtime = 235800 ;
        :nstep = 776158 ;
        :timestep = 120. ; 

但是,我必须手动打开所有文件,并手动更改文件名...数百个文件。我更喜欢制作可以自动执行此操作的bash。

我确信必须有一种更智能的方法来从nc标头中提取vdate,你们可以帮我吗?

谢谢!

bash header netcat
1个回答
0
投票

理论上,类似的方法应该起作用:

#! /bin/sh
for file in rerun4_*_spindown_* ; do

  vdate=$(ncdump -h $file | awk '$1 == ":vdate" { print $3 }')
  new_name="rerun4_spindown_$vdate.nc"
  mv "$file" "$new_name"
done

我无权访问netCDF文件-需要更多测试。

© www.soinside.com 2019 - 2024. All rights reserved.