views:

4163

answers:

7

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? I'm totally stuck. The script works great, just need it to pipe more lines to the screen. Thank you in advance for any help!..

+3  A: 

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

Phil
A: 

using tail should be the best option.

Faisal Feroz
+2  A: 

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!
Joel Spolsky
Also, OH GOD MY EYES
Joel Spolsky
Hm, that's the kind of straightforward way I never really consider with batch files. But yes, it nibbles away at the eyes.
Joey
+4  A: 

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
Patrick Cuff
I believe you can use more +n command in your example, which starts printing the file at line n
dmityugov
You can; thanks dmityugov :)
Patrick Cuff
A: 

If file is too large it can take too long to get count of lines another way is to use find and pass it a nowhere string

$find /v /c "%%$%!" yourtextfile.txt

this would result an output like this

$---------- yourtextfile.txt: 140

then you can parse output using for like this

$for /f "tokens=3" %i in ('find /v /c "%%$%!" tt.txt') do set countoflines=%i

then you can substract ten lines from the total lines

A: 

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

The following 3-liner script will list the last n lines from input file. n and file name/path are passed as input arguments.

# Script last.txt
var str file, content ; var int n, count
cat $file > $content ; set count = { len -e $content } - $n
stex -e ("["+makestr(int($count))) $content

The script is in biterscripting. To use, download biterscripting from http://www.biterscripting.com , save this script as C:\Scripts\last.txt, start biterscripting, enter the following command.

script last.txt file("c:\log09.txt") n(10)

The above will list last 10 lines from file c:\log09.txt. To list last 20 lines from the same file, use the following command.

script last.txt file("c:\log09.txt") n(20)

To list last 30 lines from a different file C:\folder1\somefile.log, use the following command.

script last.txt file("C:\folder1\somefile.log") n(30)

I wrote the script in a fairly generic way, so it can be used in various ways. Feel free to translate into another scripting/batch language.

A: 

ok sit down stand up. ok relax one minute ( turkish presidentcy ) Recep Tayip Erdogan

please explain better

sorry but I think you are asslogg shweinsteiger michael ballack lukas podolski

Deine Mutter