How to use --enable-switch in a bash script having AWK statement

时间:2016-04-25 09:11:09

标签: linux bash awk gawk

Just found out that my problem using a switch-case in "awk" is exactly same as in this knowledge base:

switch/case doesn't work in awk

Now I'm using my awk statement in a bash script and not running it as a separate AWK script file. Considering that how can I use --enable-switch in my bash script. This is the structure of my script:

#! bin/ksh
. ~/.profile
// bash if-else statements to perform some tasks
/usr/bin/awk '
BEGIN {
        // array[] defined
}

// some statements
function printmsg(msg) {
         switch(1) {
         case /pattern1/:
              print msg
              break
         case /pattern2/:
              print msg
              break
         }
}' file

Where can I enforce --enable-switch ?

My GAWK version is gawk-3.1.5-16.el5

1 个答案:

答案 0 :(得分:1)

你正在使用一个非常旧版本的gawk,默认情况下从4.0开始启用switch语句,我们现在在4.1.something上启用。您错过了很多非常有用的功能(比switch语句更有用,请参阅http://www.gnu.org/software/gawk/manual/gawk.html#Feature-History),以便获得新的gawk。

话虽如此,你可以在安装一个新的gawk之后继续使用switch(),但恕我直言,不值得让你的脚本特定于gawk,只需将其重写为if-elses,如果那样的话你正在使用的唯一gawk特有的功能。