Web Hosting Talk







View Full Version : grep regex


alanK
06-16-2006, 01:40 PM
Hi all,

I am trying to write a script that sorts a list of IP addresses, but still cannot get the regex to work. It's a bash script and the IPs are in the form of:

xxx.xxx.xxx.xxx:xxxx , :xxx is the port number.

What I am trying to do is once I get the list to count all matching IPs and list only once:

xx xxx.xxx.xxx.xxx

where xx is the number of occurrences of the xxx.xxx.xxx.xxx IP.

You can get the list if you do:

netstat --tcp -an | grep [0-9] | awk '{print$5}' | sort -n

Can someone help me with constructing the regex or give me an idea where to look for an answer? :)

Thanks

AK

Burhan
06-17-2006, 01:47 AM
Just add | uniq -c to the end, so it becomes :

netstat --tcp -an | grep [0-9] | awk '{print$5}' | sort -n | uniq -c

alanK
06-17-2006, 05:09 AM
Thanks. That's exactly what I was looking for :)