Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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



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
up vote 1 down vote accepted

Use the ~ regex-match operator

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

Your Answer


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.