Based on the input you supplied, the output should be: Code: 1 9 3 6
1 9 3 6
awk ' /a/ { a = $2 } /d/ { d = $2 } /^$/ && d == 4 && a > 0 { print a a = -1 d = -1 } END { if ( d == 4 && a > 0 ) print a } ' "$FILE"