Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

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?

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

1 Answer 1

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.