我想要形式的句柄字符串:
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?
答案 0 :(得分:3)
不完全那样,但非常接近,因为有一个正则表达式匹配运算符(〜):
BEGIN { FS="," }
/PREFIX/ {
if ($1 ~ "TYPE_1") {printf "[TYPE1] [DATA: $2]"} // <-- included pattern
if ($1 ~ "TYPE_2") {printf "[TYPE2] [DATA: $2]"} // <-- another included pattern
... // <-- some more included patterns
}
请注意,因为第一个模式匹配已经进入其块只处理了一行,所以在块中只有ifs就可以了。
如果你真的想要图案的易读性,你可以这样做:
BEGIN { FS="," }
/PREFIX/ { //stuff to do for all prefixes, before specific handling
data = $2 }
/PREFIX_TYPE_1/ { type = "TYPE_1"; }
/PREFIX_TYPE_2/ { type = "TYPE_2"; }
/PREFIX/ { //stuff to do for all prefixes, after specific handling
printf("[%s] [DATA: $2]", type, data)
}
答案 1 :(得分:1)
你可以用gawk这样做:
awk 'BEGIN { FS="," }
/PREFIX/ {
if (/TYPE_1/) {printf "[TYPE1] [DATA: $2]"} // <-- included pattern
if (/TYPE_2/) {printf "[TYPE2] [DATA: $2]"} // <-- another included pattern
... // <-- some more included patterns
}' "filename"
此处/TYPE_1/
相当于$0 ~ /TYPE_1/
。在documentation(第6.1.2部分)中查找详细信息。