【问题标题】:How to use 'Find' linux command for searching some of the variables in NetCDF files?如何使用 'Find' linux 命令来搜索 NetCDF 文件中的一些变量?
【发布时间】:2019-06-06 05:24:10
【问题描述】:

我有许多包含 NetCDF (.nc) 文件的文件夹。我只想选择那些包含所需变量的变量。看来,Linux find 命令具有此功能,但我没有找到正确的描述。如果有人有经验,那可能会很有帮助。

【问题讨论】:

  • 你有什么命令可以在单个文件中找到它?
  • 请告诉我们您所说的“.nc 文件”和“必需变量”是什么意思。
  • 很抱歉描述得不够好。 .nc 文件是 netcdf 文件,在这个文件中它有一些变量,比如“temp”。
  • Stack Overflow 是一个编程和开发问题的网站。对于这个问题,您可能应该使用Stack Exchange network 上的另一个站点。如果您觉得这个问题很切题,请参阅How to create a Minimal, Complete, and Verifiable example。找到一个例子应该没​​有问题。问题和答案遍布网络。热门搜索是How to find binary files in a directory?

标签: linux find netcdf


【解决方案1】:

试试这个,

# Set the required vars
REQ_VARS=( var1 var2 var3 )

# Loop through all .nc files found by find command defined after "done"
while IFS= read -r f; do
    ALL_VARS_FOUND=true

    # Loop through required vars and check if the .nc file contains it
    # If not, set ALL_VARS_FOUND to false and break the loop
    for VAR in "${REQ_VARS[@]}"; do
        ncinfo -v "$VAR" "$f" &> /dev/null || { ALL_VARS_FOUND=false; break; }
    done

    # Print the filename if ALL_VARS_FOUND is true.
    [ $ALL_VARS_FOUND == true ] && echo "$f"
done < <(find . -name "*.nc")

如果你只有一个变量要检查,你可以使用find -exec

VAR=var1
find . -name "*.nc" \
  -exec sh -c 'ncinfo -v "$2" "$1" 1> /dev/null 2>&1' _ {} "$VAR" \; -print

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-02-16
    • 2011-05-26
    • 1970-01-01
    • 2015-11-15
    • 1970-01-01
    • 2015-02-19
    • 2019-12-24
    相关资源
    最近更新 更多