Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

I would like to get netstat to not display port numbers on the foreign address so I can run some statistics on it. This is for a FreeBSD system.

The following is a example of the output.

<root>:/# netstat -an | grep .80 |head
tcp4       0      0  61.129.65.176.80      123.120.207.172.51972   ESTABLISHED
tcp4     491      0  61.129.65.176.80      171.250.180.211.51000  ESTABLISHED
tcp4     286      0  61.129.65.176.80      123.120.207.17210399     ESTABLISHED
tcp4     299      0  61.129.65.176.80      211.8.128.46.35458    ESTABLISHED
tcp4       0      0  61.129.65.176.80      123.139.147.112.62778    ESTABLISHED
tcp4     361      0  61.129.65.176.80      239.187.139.47.17607  ESTABLISHED
tcp4     509      0  61.129.65.176.80      74.74.87.36.7822   ESTABLISHED
tcp4     324      0  61.129.65.176.80      75.30.126.198.60721     ESTABLISHED
tcp4     508      0  61.129.65.176.80      149.78.116.66.12120   ESTABLISHED
tcp4     321      0  61.129.65.176.80      48.150.75.171.2617     ESTABLISHED
<root>:/# 
share|improve this question
1  
You can filter your output using a tool like sed, awk, or cut. Why not post some sample netstat raw output, and then an example of what you want to see? That will help others understand more precisely what you need. –  mrb Aug 28 '12 at 13:42
add comment

1 Answer

up vote 1 down vote accepted

Add this sed command at the end of your pipe. It does a greeding search until last . and delete it and all digits that follow it.

... | sed -e 's/^\(.*\)\.[0-9]*/\1/'

It yields:

tcp4       0      0  61.129.65.176.80      123.120.207.172   ESTABLISHED
tcp4     491      0  61.129.65.176.80      171.250.180.211  ESTABLISHED
tcp4     286      0  61.129.65.176.80      123.120.207.172     ESTABLISHED
tcp4     299      0  61.129.65.176.80      211.8.128.46    ESTABLISHED
tcp4       0      0  61.129.65.176.80      123.139.147.112    ESTABLISHED
tcp4     361      0  61.129.65.176.80      239.187.139.47  ESTABLISHED
tcp4     509      0  61.129.65.176.80      74.74.87.36   ESTABLISHED
tcp4     324      0  61.129.65.176.80      75.30.126.198     ESTABLISHED
tcp4     508      0  61.129.65.176.80      149.78.116.66   ESTABLISHED
tcp4     321      0  61.129.65.176.80      48.150.75.171     ESTABLISHED
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.