awk OR statement

后端 未结 3 1745
闹比i
闹比i 2021-02-02 06:32

Does awk have an OR statement i.e given the following snippet:

awk \'{if ($2==\"abc\") print \"blah\"}\'

Is it possible t

相关标签:
3条回答
  • 2021-02-02 06:48
    awk '{if ($2=="abc" || $2=="def") print "blah"}'
    
    0 讨论(0)
  • 2021-02-02 06:55

    Yes. There's logical OR || that you can use:

    awk '{if ($2=="abc" || $2=="def") print "blah" }'
    
    0 讨论(0)
  • 2021-02-02 07:05

    You would not write this code in awk:

    awk '{if ($2=="abc") print "blah"}'
    

    you would write this instead:

    awk '$2=="abc" {print "blah"}'
    

    and to add an "or" would be either of these depending on what you're ultimately trying to do:

    awk '$2~/^(abc|def)$/ {print "blah"}'
    
    awk '$2=="abc" || $2=="def" {print "blah"}'
    
    awk '
    BEGIN{ split("abc def",tmp); for (i in tmp) targets[tmp[i]] }
    $2 in targets {print "blah"}
    '
    

    That last one would be most appropriate if you have several strings you want to match.

    0 讨论(0)
提交回复
热议问题