Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Counting number of occurances 1

Status
Not open for further replies.

netman4u

Technical User
Mar 16, 2005
176
US
Hello all,

I have a script where I am running a very large text file being writen into an array. The seven element of that array is a hostname. There are many entries with the same hostname in the file. I want to count the occurances of each hostname and end up with an output like:

hostname1 1089
hostname2 12098
hostname3 10
.
.
.
.
etc.

This is what I have so far:

Code:
my @arry = `$OV_BIN/ovdumpevents 2>&1`;
	foreach  (@arry) {
		print LOG "$_\n";
	}

Obviously not much.

Here is a sample of the results of the command:

Code:
1184594414 1 Mon Jul 16 10:00:14 2007 csabxxxxx.private.company.com  - Received event Ent-.1.3.6.1.4.1.9.1.501. 4 args: [1] mgmt.mib-2.interfaces.ifTable.ifEntry.ifIndex.140 (Integer): 140  [2] mgmt.mib-2.interfaces.ifTable.ifEntry.ifDescr.140 (OctetString): GigabitEthernet5/41  [3] mgmt.mib-2.interfaces.ifTable.ifEntry.ifType.140 (Integer): ethernetCsmacd  [4] private.enterprises.cisco.local.linterfaces.lifTable.lifEntry.locIfReason.140 (OctetString): up;1 .1.3.6.1.6.3.1.1.5.4.1.3.6.1.4.1.9.1.501 0

1184594414 1 Mon Jul 16 10:00:14 2007 csab2yyyy.private.company.com  - Received event Ent-.1.3.6.1.4.1.9.1.501. 4 args: [1] mgmt.mib-2.interfaces.ifTable.ifEntry.ifIndex.140 (Integer): 140  [2] mgmt.mib-2.interfaces.ifTable.ifEntry.ifDescr.140 (OctetString): GigabitEthernet5/41  [3] mgmt.mib-2.interfaces.ifTable.ifEntry.ifType.140 (Integer): ethernetCsmacd  [4] private.enterprises.cisco.local.linterfaces.lifTable.lifEntry.locIfReason.140 (OctetString): up;1 .1.3.6.1.6.3.1.1.5.4.1.3.6.1.4.1.9.1.501 0

1184594416 1 Mon Jul 16 10:00:16 2007 csab3zzzz.private.company.com   - Received event Ent-.1.3.6.1.4.1.9.1.501. 4 args: [1] mgmt.mib-2.interfaces.ifTable.ifEntry.ifIndex.28 (Integer): 28  [2] mgmt.mib-2.interfaces.ifTable.ifEntry.ifDescr.28 (OctetString): GigabitEthernet3/25  [3] mgmt.mib-2.interfaces.ifTable.ifEntry.ifType.28 (Integer): ethernetCsmacd  [4] private.enterprises.cisco.local.linterfaces.lifTable.lifEntry.locIfReason.28 (OctetString): up;1 .1.3.6.1.6.3.1.1.5.4.1.3.6.1.4.1.9.1.501 0

Thanks in advance.

Nick


If at first you don't succeed, don't try skydiving.
 
I think you can figure this one out. You have the option of using a regexp, or split(), or possibly substr() or unpack() to get the value of the hostname into a hash that you can then use to count the number of occurances.

------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
Thanks for the reply Kevin. Your right I should have given more effort before posting my problem. It should be noted however that my grey matter does not hold info as long as it used to in my younger days, especially after not really coding for 6 months or so! ;-)

That being said here is what I have come up with based on your suggestion:

Code:
open (LOG, ">$HOME/dwdbcount.log") or die ("Error opening log file: $1");
print LOG "Program started $startime\n\n";
my %devices;
my $node =0;
my @arry = `$OV_BIN/ovdumpevents 2>&1`;
	foreach  (@arry) {
		next unless (/authenticationFailure/);
		my @entry = split;
		$devices{$entry[7]}=($node++);
}
while (my($key, $value) = each(%devices))
{
  print LOG $key ."\t" . '-' . "\t". $value . "\n";
}
my $endtime = `"date"`;
print LOG "Program completed $endtime\n";
close (LOG) or die ("Cannot close dwdbcount.log: $1" );
exit 0;

My only question is in the initialization of my counter $node = 0; I'm not really sure if it is counting correctly.

Thanks,

Nick



If at first you don't succeed, don't try skydiving.
 
if all you want is to count that one thing:

Code:
open (LOG, ">$HOME/dwdbcount.log") or die ("Error opening log file: $1");
print LOG "Program started $startime\n\n";
my %devices;
my @arry = `$OV_BIN/ovdumpevents 2>&1`;
foreach  (@arry) {
    next unless (/authenticationFailure/);
    [b]$devices{(split)[7]}++;[/b]
}
while (my($key, $value) = each(%devices))
{
  print LOG $key ."\t" . '-' . "\t". $value . "\n";
}
my $endtime = `"date"`;
print LOG "Program completed $endtime\n";
close (LOG) or die ("Cannot close dwdbcount.log: $1" );
exit 0;

------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top