I'm searching for port numbers with grep (in a bash script)
portstr=$(lsof -i -P -n | grep LISTEN | grep sshd)
portstr now looks something like this
sshd 673 root 3u IPv4 14229 0t0 TCP *:22 (LISTEN)
sshd 673 root 4u IPv6 14231 0t0 TCP *:22 (LISTEN)
now I want to extract the numbers between the colon (:) and the following blank space, to get something like this
portarray[0]=>22
portarray[1]=>22
thank you
I tried this
var="[a1] [b1] [123] [Text text] [0x0]"
regex='\[([^]]*)\](.*)'
while [[ $var =~ $regex ]]; do
arr+=("${BASH_REMATCH[1]}")
var=${BASH_REMATCH[2]}
done
from here. But nothing really worked out.