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

88,400

Solution 1

Hopefully this will save Joel's eyes :)

@echo OFF

:: Get the number of lines in the file
set LINES=0
for /f "delims==" %%I in (data.txt) do (
    set /a LINES=LINES+1
)

:: Print the last 10 lines (suggestion to use more courtsey of dmityugov)
set /a LINES=LINES-10
more +%LINES% < data.txt

Solution 2

This answer combines the best features of already existing answers, and adds a few twists.

The solution is a simple batch implementation of the tail command.

The first argument is the file name (possibly with path information - be sure to enclose in quotes if any portion of path contains spaces or other problematic characters).

The second argument is the number of lines to print.

Finally any of the standard MORE options can be appended: /E /C /P /S /Tn. (See MORE /? for more information).

Additionally the /N (no pause) option can be specified to cause the output to be printed continuosly without pausing.

The solution first uses FIND to quickly count the number of lines. The file is passed in via redirected input instead of using a filename argument in order to eliminate the printout of the filename in the FIND output.

The number of lines to skip is computed with SET /A, but then it resets the number to 0 if it is less than 0.

Finally uses MORE to print out the desired lines after skipping the unwanted lines. MORE will pause after each screen's worth of lines unless the output is redirected to a file or piped to another command. The /N option avoids the pauses by piping the MORE output to FINDSTR with a regex that matches all lines. It is important to use FINDSTR instead of FIND because FIND can truncate long lines.

:: tail.bat File Num [/N|/E|/C|/P|/S|/Tn]...
::
::   Prints the last Num lines of text file File.
::
::   The output will pause after filling the screen unless the /N option
::   is specified
::
::   The standard MORE options /E /C /P /S /Tn can be specified.
::   See MORE /? for more information
::
@echo OFF
setlocal
set file=%1
set "cnt=%~2"
shift /1
shift /1
set "options="
set "noPause="
:parseOptions
if "%~1" neq "" (
  if /i "%~1" equ "/N" (set noPause=^| findstr "^") else set options=%options% %~1
  shift /1
  goto :parseOptions
)
for /f %%N in ('find /c /v "" ^<%file%') do set skip=%%N
set /a "skip-=%cnt%"
if %skip% lss 0 set skip=0
more +%skip% %options% %file% %noPause%

Solution 3

You should probably just find a good implementation of tail. But if you really really insist on using CMD batch files and want to run on any NT machine unmolested, this will work:

@echo off
setLocal EnableDelayedExpansion
for /f "tokens=* delims= " %%a in (c:\tmp\foo.txt) do (
set var9=!var8!
set var8=!var7!
set var7=!var6!
set var6=!var5!
set var5=!var4!
set var4=!var3!
set var3=!var2!
set var2=!var1!
set var1=!var!
set var=%%a
)
echo !var9!
echo !var8!
echo !var7!
echo !var6!
echo !var5!
echo !var4!
echo !var3!
echo !var2!
echo !var1!
echo !var!

Solution 4

There are several windows implementations of the tail command. It should be exactly what you want.

This one sounds particularly good: http://malektips.com/xp_dos_0001.html

They range from real-time monitoring to the last x lines of the file.

Edit: I noticed that the included link is to a package It should work, but here are some more versions:

http://www.lostinthebox.com/viewtopic.php?f=5&t=3801 http://sourceforge.net/projects/tailforwin32

Solution 5

After trying all of the answers I found on this page none of them worked on my file with 15539 lines.

However I found the answer here to work great. Copied into this post for convenience.

@echo off
for /f %%i in ('find /v /c "" ^< C:\path\to\textfile.txt') do set /a lines=%%i
set /a startLine=%lines% - 10
more /e +%startLine% C:\path\to\textfile.txt

This code will print the last 10 lines in the "C:\path\to\textfile.txt" file.

Credit goes to OP @Peter Mortensen

Share:
88,400
Admin
Author by

Admin

Updated on July 09, 2022

Comments

  • Admin
    Admin almost 2 years

    Any ideas how to echo or type the last 10 lines of a txt file?

    I'm running a server change log script to prompt admins to state what they're doing, so we can track changes. I'm trying to get the script to show the last 10 entries or so to give an idea of what's been happening recently. I've found a script that deals with the last line, as shown below, but can't figure out what to change in it to display the last 10 lines. Script:

    @echo off
    setLocal EnableDelayedExpansion
    for /f "tokens=* delims= " %%a in (c:\log09.txt) do (
    set var=%%a
    )
    echo !var!
    

    Example of log file:

    06/02/2009, 12:22,Remote=Workstation-9,Local=,
    mdb,bouncing box after updates,CAS-08754,
    =================
    07/02/2009, 2:38,Remote=,Local=SERVER1,
    mdb,just finished ghosting c drive,CAS-08776,
    =================
    07/02/2009, 3:09,Remote=,Local=SERVER1,
    mdb,audit of server,CAS-08776,
    

    Any thoughts? The script works great, just need it to pipe more lines to the screen.