Get last n lines or bytes of a huge file in Windows (like Unix's tail). Avoid time consuming options Get last n lines or bytes of a huge file in Windows (like Unix's tail). Avoid time consuming options powershell powershell

Get last n lines or bytes of a huge file in Windows (like Unix's tail). Avoid time consuming options


If you have PowerShell 3 or higher, you can use the -Tail parameter for Get-Content to get the last n lines.

Get-content -tail 5 PATH_TO_FILE;

On a 34MB text file on my local SSD, this returned in 1 millisecond vs. 8.5 seconds for get-content |select -last 5


How about this (reads last 8 bytes for demo):

$fpath = "C:\10GBfile.dat"$fs = [IO.File]::OpenRead($fpath)$fs.Seek(-8, 'End') | Out-Nullfor ($i = 0; $i -lt 8; $i++){    $fs.ReadByte()}

UPDATE. To interpret bytes as string (but be sure to select correct encoding - here UTF8 is used):

$N = 8$fpath = "C:\10GBfile.dat"$fs = [IO.File]::OpenRead($fpath)$fs.Seek(-$N, [System.IO.SeekOrigin]::End) | Out-Null$buffer = new-object Byte[] $N$fs.Read($buffer, 0, $N) | Out-Null$fs.Close()[System.Text.Encoding]::UTF8.GetString($buffer)

UPDATE 2. To read last M lines, we'll be reading the file by portions until there are more than M newline char sequences in the result:

$M = 3$fpath = "C:\10GBfile.dat"$result = ""$seq = "`r`n"$buffer_size = 10$buffer = new-object Byte[] $buffer_size$fs = [IO.File]::OpenRead($fpath)while (([regex]::Matches($result, $seq)).Count -lt $M){    $fs.Seek(-($result.Length + $buffer_size), [System.IO.SeekOrigin]::End) | Out-Null    $fs.Read($buffer, 0, $buffer_size) | Out-Null    $result = [System.Text.Encoding]::UTF8.GetString($buffer) + $result}$fs.Close()($result -split $seq) | Select -Last $M

Try playing with bigger $buffer_size - this ideally is equal to expected average line length to make fewer disk operations. Also pay attention to $seq - this could be \r\n or just \n.This is very dirty code without any error handling and optimizations.


When the file is already opened, it's better to use

Get-Content $fpath -tail 10

because of "exception calling "OpenRead" with "1" argument(s): "The process cannot access the file..."