Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to check if awk array is empty

Tags:

linux

unix

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 tried something like this and its not working. Suggestions?

for (var in sourceAndRestricted){
    if (var > 1){
        printf "\t\t"var"\n" 
    }
    else {
        print "NONE"
    }
}
like image 205
Vinnie Biros Avatar asked Dec 01 '22 02:12

Vinnie Biros


1 Answers

Check it with length() function:

if ( length(sourceAndRestricted) > 0 ) {
    printf "\t\t"var"\n"
}
else
    print "NONE"
}
like image 86
Birei Avatar answered Dec 04 '22 05:12

Birei