Efficiently find missing numbers in a sequence

This awk will print out the numbers missing in an ordered sequence that starts with the number you set “a=” to in the BEGIN block.

awk 'BEGIN {a=1} { while (a++ < $1) {print a-1} }'

For example, here I have a few calls to seq create a sequence of integers starting at 1 and having gaps.

$ (seq 1 4; seq 7 10; seq 15 20) | awk 'BEGIN {a=1} { while (a++ < $1) {print a-1} }' 5 6 11 12 13 14

Leave a Reply

You must be logged in to post a comment.