bashawkmawk

Errors when executing switch/case in awk command in Ubuntu and Mac


I have a very strange problem when executing following code:

awk '{ foo = 1; switch (foo) { case 1: i=i+1; break; } }' ./tcpheader.txt

Getting following error:

awk: syntax error at source line 1
 context is
    { foo = 1; switch (foo) >>>  { <<< 
awk: illegal statement at source line 1
awk: illegal statement at source line 1

Any idea what's the problem ?

Tested on Mac:

awk --version

Output:

awk version 20070501

Tested on Ubuntu:

awk -W version

Output:

mawk 1.3.3 Nov 1996, Copyright (C) Michael D. Brennan

compiled limits:
max NF             32767
sprintf buffer      2040

Solution

  • Installing gawk solved my problem:

    sudo apt-get install gawk
    

    in mac:

    sudo port install gawk