1

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?

4

2 回答 2

3

您需要在表达式$2 ~的第二部分重复。||

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

或使用正则表达式|运算符:

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

对于第一种情况,condition1 和 cnd1 结合任何东西,试试这个:

awk 'BEGIN{IGNORECASE=1} $2 ~ /(cnd|condition)1/ && $2 ~ "/"'
于 2012-12-18T15:00:19.010 回答
1

如果您正在写入文件,则可以一次性完成:

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

注意单词边界的使用,\<\>防止“ACND1”和“condition11”等内容的错误匹配

于 2012-12-18T15:26:59.717 回答