Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I need to retrieve the last n lines of huge files (1-4 Gb), in Windows 7. Due to corporate restrictions, I cannot run any command that is not built-in. The problem is that all solutions I found appear to read the whole file, so they are extremely slow.

Can this be accomplished, fast?

Notes:

  1. I managed to get the first n lines, fast.
  2. It is ok if I get the last n bytes. (I used this https://stackoverflow.com/a/18936628/2707864 for the first n bytes).

Solutions here Unix tail equivalent command in Windows Powershell did not work. Using -wait does not make it fast. I do not have -tail (and I do not know if it will work fast).

PS: There are quite a few related questions for head and tail, but not focused on the issue of speed. Therefore, useful or accepted answers there may not be useful here. E.g.,

Windows equivalent of the 'tail' command

CMD.EXE batch script to display last 10 lines from a txt file

Extract N lines from file using single windows command

https://serverfault.com/questions/490841/how-to-display-the-first-n-lines-of-a-command-output-in-windows-the-equivalent

powershell to get the first x MB of a file

https://superuser.com/questions/859870/windows-equivalent-of-the-head-c-command

like image 886
sancho.s ReinstateMonicaCellio Avatar asked Apr 08 '16 19:04

sancho.s ReinstateMonicaCellio


People also ask

Does Windows 10 have a tail command?

While Windows doesn't have a standalone utility to do what tail does, we do have the Get-Content PowerShell cmdlet which happens to have a tail parameter.

How do you tail in PowerShell?

Open it with notepad $PROFILE. Then in the text document, create a new function: function Tail ($path) { Get-content -tail 15 -path $path -wait } This way you can access the function each time you start PowerShell. This should be the accepted answer.

How do I count lines in PowerShell?

To count the total number of lines in the file in PowerShell, you first need to retrieve the content of the item using Get-Content cmdlet and need to use method Length() to retrieve the total number of lines.


1 Answers

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

like image 79
alroc Avatar answered Oct 03 '22 04:10

alroc