Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

awk extended pattern matching (embedding pattern matching in actions for already matched strings)

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?

like image 711
static Avatar asked Dec 07 '25 01:12

static


2 Answers

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)
          }
like image 105
chthonicdaemon Avatar answered Dec 08 '25 15:12

chthonicdaemon


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).

like image 25
Rorick Avatar answered Dec 08 '25 14:12

Rorick



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!