How to check if the variable value in AWK script is null or empty?

Viewed 107869
  1. I am using AWK script to process some logs.
  2. At one place I need to check if the variable value is null or empty to make some decision.

Any Idea how to achieve the same?

awk '

{
    {
       split($i, keyVal, "@")
       key=keyVal[1];
       val=keyVal[2];
       if(val ~ /^ *$/)
       val="Y";

    }

}

' File

I have tried with

1) if(val == "")

2) if(val ~ /^ *$/)

not working in both cases.

4 Answers

I accidentally discovered this less-used function specific in gawk that could help differentiate :

****** gawk-only ******

BEGIN {
    $0 = "abc"

    print NF, $0

    test_function()
    test_function($(NF + 1))

    test_function("")
    test_function($0)
}
function test_function(_) { print typeof(_) }

1 abc

untyped
unassigned

string
string

So it seems, for non-numeric-like data :

  absolutely no input to function at all         :    untyped
  non-existent or empty field,      including $0 : unassigned
  any non-numeric-appearing string, including "" :     string

Here's the chaotic part - numeric data :

  • strangely enough, for absolutely identical input, only differing between using $0 vs. $1 in function call, you frequently get a different value for typeof()

  • even a combination of both leading and trailing spaces doesn't prevent gawk from identifying it as strnum

[123]:NF:1
                 $0 =  number:123          $1 =  strnum:123           +$1 =  number:123
[ 456.33]:NF:1
                 $0 =  string: 456.33      $1 =  strnum:456.33        +$1 =  number:456.33000
[ 19683 ]:NF:1
                 $0 =  string: 19683       $1 =  strnum:19683         +$1 =  number:19683

[-20.08554]:NF:1
                 $0 =  number:-20.08554    $1 =  strnum:-20.08554     +$1 =  number:-20.08554

+/- inf/nan (same for all 4):

[-nan]:NF:1

                 $0 =  string:-nan         $1 =  strnum:-nan          +$1 =  number:-nan

this one is a string because it was made from sprintf() :

[0x10FFFF]:NF:1
               $0 = string:0x10FFFF $1 = string:0x10FFFF +$1 = number:0

using -n / --non-decimal-data flag, all stays same except

[0x10FFFF]:NF:1
               $0 = string:0x10FFFF $1 = strnum:0x10FFFF +$1 = number:1114111

Long story short, if you want your gawk function to be able to differentiate between

  • empty-string input (""), versus

  • actually no input at all

    • e.g. when original intention is to directly apply changes to $0

then typeof(x) == "untyped" seems to be the most reliable indicator.

It gets worse when null-string padding versus a non-empty string of all zeros ::

function   __(_) { return (!_) ":" (!+_)  }
function  ___(_) { return (_ == "")       }
function ____(_) { return (!_) ":" (!""_) }


     $0--->[           "000" ] | __(""$0)-->{ !(""$0) : !+(""$0) }-->[ 0:1 ] 
 ___($0)-->{ $0=="" }-->[ 0 ]  | ____($0)-->{ !   $0  :  (!""$0) }-->[ 1:1000 ]


     $0--->[              "" ] | __(""$0)-->{ !(""$0) : !+(""$0) }-->[ 1:1 ] 
 ___($0)-->{ $0=="" }-->[ 1 ]  | ____($0)-->{ !   $0  :  (!""$0) }-->[ 1:1 ]


     $0--->[      " -0.0 -0" ] | __(""$0)-->{ !(""$0) : !+(""$0) }-->[ 0:1 ] 
 ___($0)-->{ $0=="" }-->[ 0 ]  | ____($0)-->{ !   $0  :  (!""$0) }-->[ 0:1 -0.0 -0 ]


     $0--->[          " 0x5" ] | __(""$0)-->{ !(""$0) : !+(""$0) }-->[ 0:1 ] 
 ___($0)-->{ $0=="" }-->[ 0 ]  | ____($0)-->{ !   $0  :  (!""$0) }-->[ 0:1 0x5 ]
Related