print current and previous line only if previous line fulfills a condition

# conditions: same key, num is bigger or equal 2 and num is lower than previous one
 
cat <<EOF | awk 'NR>1 && prev_key==$2 && $3>=2 && prev_num<$3 { print prev_line"\n"$0 } { prev_line=$0; prev_key=$2; prev_num=$3 }' | uniq 
line    key     num
1       A       0 
2       A       1
3       A       0
4       A       1
5       A       2 <= print current line and previous line
6       B       3
7       B       1
8       B       0
9       B       1
10      B       2 <= print current line and previous line
11      B       3 <= print current line and previous line. duplicated removed by uniq command
12      B       1
EOF

Leave a comment