Is there a way to simulate the *nix tail command on the Windows command line? I have a file and I want a way to snip off the first n lines of text. For example:
Here is a fast native head command that gives you the first 9 lines in DOS.
findstr /n "." myfile.txt | findstr "^.:"
The first 2 characters on each line will be the line number.
FWIW, for those just needing to snip off an indeterminate number of records from the head of the file, more > works well. This is useful just to have a smaller file to work with in the early stages of developing something.
IF you have Windows PowerShell installed (I think it's included since XP) you can just run from cmd.exe:
Head Command:
powershell -command "& {Get-Content *filename* -TotalCount *n*}"
Tail Command:
powershell -command "& {Get-Content *filename* | Select-Object -last *n*}"
or, directly from PowerShell:
Get-Content *filename* -TotalCount *n*
Get-Content *filename* | Select-Object -last *n*
update
PowerShell 3.0 (Windows 8 and higher) added Tail
command with alias Last
.
Head
and First
aliases to TotalCount
were also added.
So, commands can be re-written as
Get-Content *filename* -Head *n*
Get-Content *filename* -Tail *n*
Well, this will do it, but it's about as fast as it looks (roughly O(n*m), where n is the number of lines to display and m is the total number of lines in the file):
for /l %l in (1,1,10) do @for /f "tokens=1,2* delims=:" %a in ('findstr /n /r "^" filename ^| findstr /r "^%l:"') do @echo %b
Where "10" is the number of lines you want to print, and "filename" is the name of the file.
Powershell:
Get-Content C:\logs\result.txt -Tail 10
Get-Content C:\logs\result.txt -wait (monitor the tail)
There is a resource kit that can be downloaded from here: http://www.microsoft.com/downloads/en/confirmation.aspx?familyId=9d467a69-57ff-4ae7-96ee-b18c4790cffd&displayLang=en
It contains a tail.exe tool but it is only compatible with some Windows versions
(Copied from this post: Tail command for windows)