Reputation: 8396
I want handle strings of the form:
PREFIX_TYPE_N,DATA
So, does the *awk (gawk, mawk, nawk) support including pattern matching in the action for already matched string? Something like this (of course, doesn't work for me):
*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"
Or do I still need if/else or switch/case?
Upvotes: 3
Views: 916
Reputation: 19810
Not quite in that way, but pretty close, as there is a regular expression match operator (~):
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
}
Note that because the first pattern match will already enter its block with only one row processed, it's fine to have only ifs in the block.
If you really want the legibility of patterns, you could do this:
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)
}
Upvotes: 3
Reputation: 8953
You can do this way in 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"
Here /TYPE_1/
is equivalent of $0 ~ /TYPE_1/
. Look for details in documentation (part 6.1.2).
Upvotes: 1