Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
I have a requirment when in i need to get the last N number of line from a file and do a comparision.... can u guide me on how to get the last N lines from a file

@echo off > newfile & setLocal enableDELAYedexpansion
set N=
for /f "tokens=* delims= " %%a in (myfile) do (
set /a N+=1
)
set /a X=!N!-10
set N=for /f "tokens=* delims= " %%a in (myfile) do (
set /a N+=1
if !N! gtr !X! echo %%a >> newfile
)
=====================================
Helping others achieve escape felicityM2

Alternatively use the appropriate tool for the job. Tail.exe is available either as part of the Windows Server 2003 Resource Kit Tools from Microsoft, or the GNU Win32 port.

maybe to illustrate
for ( .... myfile ) do (
# set variable for 10 lines everytime
set var1
set var2
set var3
....
.....
set var10
)at the end of the loop, those vars will contain the last 10 lines...

Hi ghostdog,
Thanks
I gotta think about this
=====================================
Helping others achieve escape felicityM2

if you have an old edlin.com or edlin.exe, you can pipe
the following into it (from batchfile tail.bat which is its
own pipe, it goto's past the pipe data)
:batchfile tail.bat:
goto dd
#
-111,.d
e
:dd
edlin %1 < tail.bat(the "#" tells it goto the last line, then it "backs up"
11 lines, (the next blank line is important, be sure
to put it there)
then it deletes from 1 to "." which ref's the
line it's sitting on, then "e" saves the file.)
if you wanted to make the number of lines as a
variable quantity, you could use eko.com to put
the variable line-count data into a pipe file.
also, if using large files, you will have to buffer through
the file, which i will explain if any interest is expressed
in this post, otherwise i'll wait to see if any cares.

![]() |
![]() |
![]() |
| Login or Register to Reply | |
| Login | Register |
| Ads by Google |