Asis Asis - 1 year ago 112
Bash Question

How to print the IP and the Port separately with netstat?

I want to print the IP and the Port separately using


I tried this:

netstat -nat | awk '{print $4}'

But It gives me that:

I want something like that:

First the IP:

and with another command the Port:

Answer Source

If you want them as different commands, you could use sed to do it like:

netstat -nat | awk '{print $4}' | sed -e 's/:.*//' # gives IP only
netstat -nat | awk '{print $4}' | sed -e 's/.*://' # gives port only

Depending on how you're using it, you could store it in a bash variable and accomplish the same thing while access it like

both=$(netstat -nat | awk '{print $4}')
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download