如何检查awk数组是否为空 [英] how to check if awk array is empty

查看:636
本文介绍了如何检查awk数组是否为空的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是AWK的新手,并试图确定我的数组是否为空,所以我可以打印一条消息.通常,我习惯于对函数进行长度调整,并且可以像这样进行检查,但是AWK似乎没有这些函数.这是我的工作代码,如果解析完所有数据后数组中没有任何内容,我只想打印出一条不同的消息.

I am brand new to AWK and trying to determine if my array is empty or not so i can print a message if so. Typically i am use to length functions and can check like that, but it does not seem AWK has those. Here is my working code, i just want to print out a different message if there is nothing in the array after parsing all my data.

#add to array if condition is met
if ($2 == "SOURCE" && $4 == "RESTRICTED"){
    sourceAndRestricted[$3]++;
}
#print out array
for (var in sourceAndRestricted){
    printf "\t\t"var"\n" 
}

ive尝试了类似的操作,但无法正常工作.有建议吗?

ive tried something like this and its not working. Suggestions?

for (var in sourceAndRestricted){
    if (var > 1){
        printf "\t\t"var"\n" 
    }
    else {
        print "NONE"
    }
}

推荐答案

使用length()函数进行检查:

if ( length(sourceAndRestricted) > 0 ) {
    printf "\t\t"var"\n"
}
else
    print "NONE"
}

这篇关于如何检查awk数组是否为空的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆