如何使用“查找” Linux命令在NetCDF文件中搜索某些变量?

时间:2019-01-11 09:48:06

标签: linux find netcdf

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

1 个答案:

答案 0 :(得分: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