Ask for help about awk SUSv3 behavior

  • 質問 : 数値で扱っているか、文字列で扱っているか?

gawk では、

$ cat num.awk                                                                                
BEGIN{
a="+2"
b=2
if(a==b)
 print "numeric comparison"
else
 print "string comparison"
}                                                                                            
$ awk -f num.awk
string comparison

ただし、SUSv3 (なにそれ?) では numeric だそうな。

最近の awk は全て string なケースが増えているのですが、備忘録として以下のものは string 扱いだそうです。

   1. Field variables
   2. Input from the getline ( ) function
   3. FILENAME
   4. ARGV array elements
   5. ENVIRON array elements
   6. Array elements created by the split ( ) function
   7. A command line variable assignment
   8. Variable assignment from another numeric string variable and after
     all the following conversions have been applied, the resulting string
     would lexically be recognized as a NUMBER token as described by the
     lexical conventions in Grammar (on page 170):

    -- All leading and trailing <blank>s are discarded.
    -- If the first non-<blank> is '+' or '-', it is discarded.
    -- Changing each occurrence of the decimal point character from the
       current locale to a period.

なんとなく「よしなに」処理しているようで、ルールがあるわけですね。