Solved To get the last 10 lines of a file.

October 9, 2009 at 01:41:30
Specs: Windows XP, 2.992 GHz / 1021 MB
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

See More: To get the last 10 lines of a file.

Report •


#1
October 9, 2009 at 02:34:56
✔ Best Answer
@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 felicity

M2


Report •

#2
October 9, 2009 at 06:17:46
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.

Report •

#3
October 9, 2009 at 07:47:45
Thank Mechanixgo and klint i was able to implement it :)

Report •

Related Solutions

#4
October 10, 2009 at 03:17:27
@M2, shouldn't have to call the file 2 times. Another way is the use circular buffers.

GNU win32 packages | Gawk


Report •

#5
October 10, 2009 at 05:51:55
circular buffers?


=====================================
Helping others achieve escape felicity

M2


Report •

#6
October 10, 2009 at 07:31:57
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...

GNU win32 packages | Gawk


Report •

#7
October 10, 2009 at 07:44:57
Hi ghostdog,

Thanks

I gotta think about this


=====================================
Helping others achieve escape felicity

M2


Report •

#8
October 11, 2009 at 17:32:09
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
#
-11

1,.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.


Report •


Ask Question