Monday, September 26, 2016

powershell: Script to count the number of lines appended into the file in last one hour

Situation: Suppose you have a log file which gets updated with logs continuously and you need to count the number of lines appended into the file in last one hour.

There could be many approaches to it, but I took the longest one.

Note: I have been asked by few to read just the last line from a file. Coincidentally the below script uses an one liner solution to read only the last line from a file using PowerShell.

Here is the One Liner to get the last line from a file.

$LastLine = Get-Content "PathOfFileToRead" | Select-Object -last 1


Here is my approach to get the count of lines added into the dynamically updating log file.
############################################################################# 
## 
## Lines Count
## Created by Ataul Haque
## Date : 23 Sep 2016 
## Version : 1.0 
## Email: ataulhaque@outlook.com  
############################################################################## 
#
$LogFile = "C:\ataul\raw.txt"  
# To count the number of lines in file, actually a large one
$count = 0
$reader = New-Object IO.StreamReader $LogFile
while($reader.ReadLine() -ne $null){ $count++ }
$reader.Close()
#Get the Last Line content to get the last time stamp from the line and parse it
$LastLine = Get-Content $LogFile | Select-Object -last 2
$items = $LastLine -split '\s+'
#get the hour from the time stamp
$LastLogHour = $items[2].split(':')
[int]$intNum = [convert]::ToInt32($LastLogHour[0], 10)
#get one hour before time stamp
$lastHour = [int]$intNum - 1;
#frame a time stamp of one hour before
$lastHourLogLine = [string]$lastHour+":"+$LastLogHour[1]+":"+$LastLogHour[2]
#search the pattern of subtracted time stamp and find that pattern
$LineNumber = Select-String -Path $LogFile -Pattern $lastHourLogLine | Select-Object -First 1 | Select-Object -ExpandProperty 'LineNumber'
#Subtract the lines count and get the new lines appended into log file
[int]$LinesAdded = [int]$count - [int]$LineNumber
[string]$Outputreport = "Logs generated in Last One Hour: $LinesAdded";
echo $Outputreport


Do let me know if you find this solution suitable to your situation and if not you can write your specific query in the comments section below, I may attempt to solve that as well.

No comments:

Post a Comment