TL;DR
for i in {1..1024}; do nc localhost $i -zv; done 2>&1 | grep Connected | sed 's/.:(.)./\1/g'
Example
$ for i in {1..1024}; do nc localhost $i -zv; done 2>&1 | grep Connected | sed 's/.:(.)./\1/g'
22
80
443
for i in {1..1024}; do nc localhost $i -zv; done 2>&1 | grep Connected | sed 's/.:(.)./\1/g'
$ for i in {1..1024}; do nc localhost $i -zv; done 2>&1 | grep Connected | sed 's/.:(.)./\1/g'
22
80
443