Unix Shell - counting ip addresses

This is Interesting: Free IT Magazines  
Home > Archive > Unix Shell > October 2006 > counting ip addresses





You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

Author counting ip addresses
rogv24@yahoo.com

2006-10-27, 1:17 pm

I am trying to write a routine to provide counts from a list of IP
addresses that are sorted.

example:

2.3.4.5
2.3.4.5
2.3.4.5
10.20.20.40
10.20.20.40

report would like this 2.3.4.5 - sum 3
10.20.20.40 - sum 2

thanks

Eric Moors

2006-10-27, 1:17 pm

rogv24@yahoo.com wrote:

> I am trying to write a routine to provide counts from a list of IP
> addresses that are sorted.
>
> example:
>
> 2.3.4.5
> 2.3.4.5
> 2.3.4.5
> 10.20.20.40
> 10.20.20.40
>
> report would like this 2.3.4.5 - sum 3
> 10.20.20.40 - sum 2
>


I would suggest you take a look at the uniq command,
and especially to its -c flag.

Eric
rogv24@yahoo.com

2006-10-27, 1:17 pm

thank you.

Eric Moors wrote:
> rogv24@yahoo.com wrote:
>
>
> I would suggest you take a look at the uniq command,
> and especially to its -c flag.
>
> Eric


Stephan Grein

2006-10-27, 1:17 pm

rogv24@yahoo.com wrote:
> I am trying to write a routine to provide counts from a list of IP
> addresses that are sorted.
>
> example:
>
> 2.3.4.5
> 2.3.4.5
> 2.3.4.5
> 10.20.20.40
> 10.20.20.40
>
> report would like this 2.3.4.5 - sum 3
> 10.20.20.40 - sum 2
>
> thanks
>


If you don't mind Python:

#!/usr/bin/python
# emulates uniq -c
# *-* encoding: utf-8 -*-

import sys

res = {}
count = 1
for x in open(sys.argv[1], "r").readlines():
try:
res[x] += 1
except:
res[x] = 1

for key, value in res.items():
print "#%i %s occured %i times!"%(count, key.replace("\n", ""), value)
count += 1
res = {}

Or just use uniq and the -c switch!

HTH
--
Stephan Grein, <stephan at stephan minus rockt dot de>
https://stephan-rockt.de
GnuPG-Key-ID: 0xF8C275D4
FingerPrint: 5B6F 134A 189B A24D 342B 0961 8D4B 0230 F8C2 75D4
Bill Marcum

2006-10-27, 7:15 pm

On 27 Oct 2006 06:42:28 -0700, rogv24@yahoo.com
<rogv24@yahoo.com> wrote:
> I am trying to write a routine to provide counts from a list of IP
> addresses that are sorted.
>
> example:
>
> 2.3.4.5
> 2.3.4.5
> 2.3.4.5
> 10.20.20.40
> 10.20.20.40
>
> report would like this 2.3.4.5 - sum 3
> 10.20.20.40 - sum 2
>

Is this homework? It's simple in awk.


--
What kind of sordid business are you on now? I mean, man, whither
goest thou? Whither goest thou, America, in thy shiny car in the night?
-- Jack Kerouac
Sponsored Links






Free braindumps | Software forum | Database administration forum

Copyright 2003 - 2008 webservertalk.com