Error404
Error404

Reputation: 7121

awk multiple conditional command

I am trying to select some strings with awk but i am not getting exactly what i want

the data i have is in a column like this

name1    condition1
name2    condition2/condition1
name3    CONDITION3
name4    condition1/condition4
name5    CND1
name6    condition6
name7    cnd1
name8    condition3/cnd1
name9    CND1/condition2

I am trying to pick condition1 and cnd1 regardless its position and case of the letters.

I want the output to be like (condition1 and cnd1 in combination with anything)

name2    condition2/condition1
name4    condition1/condition4
name8    condition3/cnd1
name9    CND1/condition2

and another output to look like (condition1 and cnd1 ALONE)

name1    condition1
name5    CND1
name7    cnd1

I am using this command

awk 'BEGIN{IGNORECASE=1} $2 ~ /^cnd1$/ || /^condition1$/'  directory/file.tab

this command is eliminating all the combinations.

How do I form the right command for this?

Upvotes: 1

Views: 349

Answers (2)

glenn jackman
glenn jackman

Reputation: 246807

If you're writing to files, you can do it in one pass:

awk '
    BEGIN {IGNORECASE=1} 
    $2 ~ /\<(cnd|condition)1\>/ {
        if ($2 ~ /\//)
            print > combined_cnd1
        else
            print > only_cnd1
    }
'

Note the use of word boundaries \< and \> to prevent false matching for things like "ACND1" and "condition11"

Upvotes: 1

John Kugelman
John Kugelman

Reputation: 361605

You need to repeat the $2 ~ in the second part of the || expression.

awk 'BEGIN{IGNORECASE=1} $2 ~ /^cnd1$/ || $2 ~ /^condition1$/'

Or use the regex | operator:

awk 'BEGIN{IGNORECASE=1} $2 ~ /^(cnd|condition)1$/'

For the first case, condition1 and cnd1 in combination with anything, try this:

awk 'BEGIN{IGNORECASE=1} $2 ~ /(cnd|condition)1/ && $2 ~ "/"'

Upvotes: 3

Related Questions