[SOLVED] awk regex query
by uncle-c from LinuxQuestions.org on (#5FSWA)
Code:$ cat netstat.txt
tcp 0 0 192.168.119.130:52876 216.58.204.35:80 ESTABLISHED
tcp 0 0 192.168.119.130:35746 13.227.173.11:22 ESTABLISHED
tcp 0 0 192.168.119.130:59290 172.217.169.46:443 ESTABLISHED
tcp 0 0 192.168.119.130:44616 54.191.111.152:3768 ESTABLISHEDI would like to use AWK to print field 5 but only if it ends with 22,80 or 443 i.e. extract IPs which the host machine is connected to on ports 22, 80 and 443. Why then do I get the field
54.191.111.152:3768
also returned in the output ? Looks as if the regex is incorrect but I'm at odds as to why ?
Code:$ awk ' $5 ~ /[22,80,443]$/ { print $5} ' netstat.txt
216.58.204.35:80
13.227.173.11:22
172.217.169.46:443
54.191.111.152:3768Below the output is as expected
Code:$ awk ' $5 ~ /[22,443]$/ { print $5} ' netstat.txt
13.227.173.11:22
172.217.169.46:443Thanks.


tcp 0 0 192.168.119.130:52876 216.58.204.35:80 ESTABLISHED
tcp 0 0 192.168.119.130:35746 13.227.173.11:22 ESTABLISHED
tcp 0 0 192.168.119.130:59290 172.217.169.46:443 ESTABLISHED
tcp 0 0 192.168.119.130:44616 54.191.111.152:3768 ESTABLISHEDI would like to use AWK to print field 5 but only if it ends with 22,80 or 443 i.e. extract IPs which the host machine is connected to on ports 22, 80 and 443. Why then do I get the field
54.191.111.152:3768
also returned in the output ? Looks as if the regex is incorrect but I'm at odds as to why ?
Code:$ awk ' $5 ~ /[22,80,443]$/ { print $5} ' netstat.txt
216.58.204.35:80
13.227.173.11:22
172.217.169.46:443
54.191.111.152:3768Below the output is as expected
Code:$ awk ' $5 ~ /[22,443]$/ { print $5} ' netstat.txt
13.227.173.11:22
172.217.169.46:443Thanks.