awk regular expression search

Hi All,

I would like to search a regular expression by passing as an i/p variableto AWK.

For Example ::

162.111.101.209.9516 
162.111.101.209.41891
162.111.101.209.9516 
162.111.101.209.9517 
162.111.101.209.41918
162.111.101.209.9517 
162.111.101.209.41937
162.111.101.209.41951
162.111.101.209.41954

i would like to extract 9516 and 9517 i.e., pattern 951[1-9] only.

Below is the command i have tried which worked

cat File | awk -F. ' $NF ~ /951[1-9]/ {print $0 }'

But i would like to pass the search pattern as a variable. Can you please help me.

awk -F. '$NF ~ p' p='951[1-9]' infile

im working on netstat command.
can you please help me how to stream the output to above command

netstat 's output includes both local and foreign address, which one do you want?

local address

If the local address is in the first column (as on Solaris),
something like this might work:

netstat -anf inet | nawk '$1 ~ p' p='\.951[1-9]$'

If the above script errors out or doesn't produce the expected output,
post a sample of your netstat output.
What OS are you using?

yes it is working. Thanks.
How about if i want to pass a variable instead of 951[1-9]

var='\\.152[1-9]$'
netstat -anf inet | nawk '$1 ~ p' p="$var"