Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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

share|improve this question
    
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 Nov 26 '13 at 19:47
add comment

1 Answer 1

up vote 1 down vote accepted

Use the ~ regex-match operator

if ($i ~ /^[[:upper:]][[:upper:]][[:digit:]][[:digit:]]/)
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.