{ p = ($2 -$1) < 0 ? -1 : 1 safe = 1 for (i=2;i<=NF;i++) { d = ($i - $(i-1)) q = d < 0 ? -1 : 1 if (p != q || d>3 || d<-3 || d == 0) { safe = 0 break } } #print NR, $0 , p, safe S += safe } END { print S }