awk 扩展模式匹配(在已匹配字符串的操作中嵌入模式匹配)

awk 扩展模式匹配(在已匹配字符串的操作中嵌入模式匹配)

我想要处理以下形式的字符串:

PREFIX_TYPE_N,DATA

那么,*awk(gawk、mawk、nawk)是否支持在已匹配字符串的操作中包含模式匹配?像这样的东西(当然,对我不起作用):

*awk 'BEGIN { FS="," }
     /PREFIX/ {
                /TYPE_1/  {printf "[TYPE1] [DATA: $2]"}    // <-- included pattern 
                /TYPE_2/  {printf "[TYPE2] [DATA: $2]"}    // <-- another included pattern
                ...                                        // <-- some more included patterns
              }' "filename"

或者我还需要 if/else 或 switch/case 吗?

答案1

这应该可行,但我不是 awk 专家,所以如果您等待,可能会有更好的答案:

*awk 'BEGIN { FS="," }
     /PREFIX/ {
                if ($0 ~ /TYPE_1/)  {printf "[TYPE1] [DATA: $2]"}    // <-- included pattern 
                if ($0 ~ /TYPE_2/)  {printf "[TYPE2] [DATA: $2]"}    // <-- another included pattern
                ...                                        // <-- some more included patterns
              }' "filename"

相关内容