Ever want to sift through a log file and report on the top 10 occurances of a certain field value? Here is a PowerShell script that will do just that.
I had a log file that contained an IP address in the third field (column index 2 since arrays start at zero in PowerShell). I wanted to know what were the top 10 IPs that were logs. I could call this script like:
./ipcount.ps1 logfile.log 2
When using huge log files, don't forget if you want to redisplay, but not recompute, the results, you can "dot source" the script like:
. ./ipcount.ps1 logfile.log 2
Then $result will always hold the last set of results. Here is the script (3 lines...middle line is really long):
param($file,$index)
$result = gc $file | foreach {$hash=@{}}{$hash[$_.split(',')[[int]$index]] += 1}{$hash.getenumerator()} | sort value -desc | select -first 10
$result
Just some random development ramblings mostly related to the Microsoft .NET platform.
Tuesday, November 6, 2007
Friday, November 2, 2007
URLs and System.IO.Path
Cool. I just found out that the .NET System.IO.Path static methods like GetFilename and GetDirectory work with internet URLs as well. I was wanting to get just the file name from a URL and the System.Uri class doesn't do this...but Path does. Now ain't that slick.
Subscribe to:
Posts (Atom)
Can't RDP? How to enable / disable virtual machine firewall for Azure VM
Oh no! I accidentally blocked the RDP port on an Azure virtual machine which resulted in not being able to log into the VM anymore. I did ...
-
Oh no! I accidentally blocked the RDP port on an Azure virtual machine which resulted in not being able to log into the VM anymore. I did ...
-
Here is a full test program that demonstrates how to use SharpZipLib to zip an XElement into a byte array. This allows you to transfer larg...