Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I want handle strings of the form:


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?

share|improve this question

closed as too localized by Michael Mrozek Sep 5 '12 at 21:11

This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.If this question can be reworded to fit the rules in the help center, please edit the question.

Same question as this one. – Thor Sep 5 '12 at 14:49
Don't crosspost – Michael Mrozek Sep 5 '12 at 21:11
up vote 1 down vote accepted

This should work, but I'm not an expert of awk so there might be better answers if you wait:

*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"
share|improve this answer

Not the answer you're looking for? Browse other questions tagged or ask your own question.