Precising if statement in awk

1

Is that possible to precise conditions in "if" statement? Let's take an example from my previous post (by the way, "k" variable is tricky, thanks terdon;)) and say that I'm looking for $i which starts from two capital letter, after that we have 2 digits, other letters/digits are not important and lenght of the word which I'm looking for is not constant (in example: AB12asdf or HR56Ame4).

awk '{k=0; for (i=1;i<=NF;i++){

        if ($i=="ABC"){print $(i-2); k++}
       } 
       if(k==0){print "No ABC in line",NR}
 }' file.txt

Thanks,

lucas

lucas

Posted 2013-11-26T19:31:03.663

Reputation: 47

You can be as precise as the condition you write matches on. /^[[:upper:]][[:upper:]][[:digit:]][[:digit:]]/ will match two upper-case letters followed by two digits. – Etan Reisner – 2013-11-26T19:47:08.030

Answers

1

Use the ~ regex-match operator

if ($i ~ /^[[:upper:]][[:upper:]][[:digit:]][[:digit:]]/)

glenn jackman

Posted 2013-11-26T19:31:03.663

Reputation: 18 546