Computing.Net > Forums > Programming > To get the last 10 lines of a file.

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

To get the last 10 lines of a file.

Reply to Message Icon

Name: Gods
Date: October 9, 2009 at 01:41:30 Pacific
OS: Windows XP
CPU/Ram: 2.992 GHz / 1021 MB
Subcategory: Batch
Comment:

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



Sponsored Link
Ads by Google

Response Number 1
Name: Mechanix2Go
Date: October 9, 2009 at 02:34:56 Pacific
Reply:

@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


0

Response Number 2
Name: klint
Date: October 9, 2009 at 06:17:46 Pacific
Reply:

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.


0

Response Number 3
Name: Gods
Date: October 9, 2009 at 07:47:45 Pacific
Reply:

Thank Mechanixgo and klint i was able to implement it :)


0

Response Number 4
Name: ghostdog
Date: October 10, 2009 at 03:17:27 Pacific
Reply:

@M2, shouldn't have to call the file 2 times. Another way is the use circular buffers.

GNU win32 packages | Gawk


0

Response Number 5
Name: Mechanix2Go
Date: October 10, 2009 at 05:51:55 Pacific
Reply:

circular buffers?


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

M2


0

Related Posts

See More



Response Number 6
Name: ghostdog
Date: October 10, 2009 at 07:31:57 Pacific
Reply:

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


0

Response Number 7
Name: Mechanix2Go
Date: October 10, 2009 at 07:44:57 Pacific
Reply:

Hi ghostdog,

Thanks

I gotta think about this


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

M2


0

Response Number 8
Name: nbrane
Date: October 11, 2009 at 17:32:09 Pacific
Reply:

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.


0

Sponsored Link
Ads by Google
Reply to Message Icon





Use following form to reply to current message:

Login or Register to Reply
LoginRegister


Sponsored links

Ads by Google


Results for: To get the last 10 lines of a file.

Delete all but last 10 lines .txt www.computing.net/answers/programming/delete-all-but-last-10-lines-txt/17375.html

Reading the last few chars of a file... www.computing.net/answers/programming/reading-the-last-few-chars-of-a-file/1545.html

Batch to edit 1st line of txt file www.computing.net/answers/programming/batch-to-edit-1st-line-of-txt-file-/17985.html