awk OR statement

后端 未结 3 1839
闹比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 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.

提交回复
热议问题