Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Tail command to see last few lines

1,052 views
Skip to first unread message

Klaus Meinhard

unread,
Apr 29, 2005, 8:30:13 PM4/29/05
to
Another thread in amb.nt deals with the above topic using 14 lines of
not easily understandable NT cmd.exe code.

I know I am a spoil-sport, but using 4DOS this is, again, a 1-liner.

To see the last 10 lines of a file, use

tail /n10 myfile.txt

If you want to watch the last 10 lines of myfile.txt continuously, put
the tail command into a loop with DELAY like

do forever
tail /n10 myfile.txt
delay 10
enddo

Of course this batch will runder all versions of DOS, Win)n/ME and
WinNT/XP. All you need to do is download and install 4DOS.

--
* Klaus Meinhard *
www.4dos.info
- 4DOS Info -
- Info for DOS -


billious

unread,
Apr 29, 2005, 10:45:13 PM4/29/05
to

"Klaus Meinhard" <K_Mei...@t-online.de> wrote in message
news:d4ujjv$bnl$04$1...@news.t-online.com...

Klaus,
you appear to have completely missed the point of the exercise.

This constant advocacy for third-party tools, however free or stable or
slick simply denies the academic challenge of using Bill's Broken Toolkit.
Any problem presented here can be solved by an appropriate assembler
program, or an equivalent in some higher-level language.

Such solutions are ASSEMBLER solutions or WHATEVER-LANGUAGE solutions, not
batch solutions.

Your solution to problems appears to boil down to 'it's easier to use 4Dos',
which makes you sound like the unfortunate Mr. XSET of six or seven years
ago. Your evangelising is tedious. Please have the courtesy to realise that
if dos-batch regulars wanted to use 4Dos, they would - and their solutions
would then be posted in comp.os.msdos.4dos or some similar 4dos discussion
group.

...Bill


Klaus Meinhard

unread,
Apr 30, 2005, 10:50:30 AM4/30/05
to
Bill,

> you appear to have completely missed the point of the exercise.

> This constant advocacy for third-party tools, however free or stable
> or slick simply denies the academic challenge of using Bill's Broken
> Toolkit.

Oh, I know that :-)

> Any problem presented here can be solved by an appropriate
> assembler program, or an equivalent in some higher-level language.
> Such solutions are ASSEMBLER solutions or WHATEVER-LANGUAGE
> solutions, not batch solutions.

We are on a slippery slope here: many "batch" solutions of nontrivial
problems apply 3rd-party tools, from little one-off assembler solutions
via grown-up helpers like sed and awk to complete languages like basic
or pascal. My (tiresome) argument is that you don't need so many tools.

> Your solution to problems appears to boil down to 'it's easier to use
> 4Dos', which makes you sound like the unfortunate Mr. XSET of six or
> seven years ago. Your evangelising is tedious.

I may have overdone it a bit :-)

But you should realize that these 2 problems (tail and converting hex to
dec) are real problems even in the enhanced XP batch world, requiring an
certain amount of nontrivial code. They boil down to trivial solutions
when you use 4DOS.

The point is: this newsgroup is for 2 groups, a guru group of regulars
which enjoys working command.com's batch abilities and bugs through the
paces, _and_ people seeking workable solutions (they mostly don't care
about batch one way or another, they just want a solution and try their
luck in amb). For the first group the mentioning of 4DOS solutions is a
constant spoil-sport (understandably). For the second it is a hint that
there is a tool out there to enhance their productivity tenfold.

I have already written at length about the (lack of) ability of
command.com batches as sometimes produced in this group to function in a
professional environment due to their lack of maintainability, language
independancy, version independancy and adaptability, so I won't start
this again :-)

> Please have the
> courtesy to realise that if dos-batch regulars wanted to use 4Dos,
> they would - and their solutions would then be posted in
> comp.os.msdos.4dos or some similar 4dos discussion group.

I will tone it down a bit :-) , but I guess from time to time 4DOS will
get the better of me, especially when I think it will do some good :-))

fpschultze

unread,
May 7, 2005, 6:14:15 PM5/7/05
to
The batch code below does the same and requires WSH which ships with
all flavours of Windows since Windows 98 (or SE).

=====
:: Create a temp. VBS file that shows the last 10 lines of a text file
ECHO Set fso =
WScript.CreateObject("Scripting.FileSystemObject")>%TEMP%.\_.VBS
ECHO Do While fso.FileExists(WScript.Arguments(0))>>%TEMP%.\_.VBS
ECHO Set file = fso.OpenTextFile(WScript.Arguments(0),
1)>>%TEMP%.\_.VBS
ECHO content = Split(file.ReadAll, vbCrLf, -1, 1)>>%TEMP%.\_.VBS
ECHO For i = UBound(content)-10 To UBound(content)>>%TEMP%.\_.VBS
ECHO WScript.Echo content(i)>>%TEMP%.\_.VBS
ECHO Next>>%TEMP%.\_.VBS
ECHO Set file = Nothing>>%TEMP%.\_.VBS
ECHO WScript.Sleep 10000>>%TEMP%.\_.VBS
ECHO Loop>>%TEMP%.\_.VBS
:: Call the temp. VBS file for myfile.txt.
CSCRIPT //Nologo %TEMP%.\_.VBS myfile.txt
:: Next line will be executed if another process has deleted my
file.txt
IF EXIST %TEMP%.\_.VBS DEL %TEMP%.\_.VBS
=====

Regards

Frank-Peter Schultze [http://www.fpschultze.de]

Timo Salmi

unread,
May 8, 2005, 1:09:19 AM5/8/05
to
fpschultze wrote:
> The batch code below does the same and requires WSH which ships
> with all flavours of Windows since Windows 98 (or SE). :: Create a

> temp. VBS file that shows the last 10 lines of a text file

> ECHO content = Split(file.ReadAll, vbCrLf, -1, 1)>>%TEMP%.\_.VBS

Scripting Runtime Library documentation: "For large files,
using the ReadAll method wastes memory resources. Other techniques
should be used to input a file, such as reading a file line by line."

Here is an alternative, written for XP

A Visual Basic Script (VBScript) aided command line script version
of "tail"
@echo off & setlocal enableextensions
::
:: Make a test file
for %%i in (1 2 3 4 5 6) do echo This is line %%i>>MyFile.txt
echo.>>MyFile.txt
for %%i in (8 9 10 11 12) do echo This is line %%i>>MyFile.txt
::
:: Build a Visual Basic Script
set skip=
set vbs_=%temp%\vtail.vbs
findstr "'%skip%VBS" "%~f0" > %vbs_%
::
:: Run the script with Microsoft Windows Script Host Version 5.6
cscript //nologo %vbs_% "MyFile.txt" 3
::
:: Clean up
for %%f in (%vbs_% MyFile.txt) do del %%f
endlocal & goto :EOF
'
'.......................................................
'The Visual Basic Script
'
' Define some constant
Const ForReading = 1, ForWriting = 2, ForAppending = 8 'VBS
Dim fileName, FSO, f, i, n, nArg, s 'VBS
'
' Check the right usage
nArg = WScript.Arguments.Count 'VBS
If (nArg = 0) Or (nArg > 2) Then 'VBS
s = "Usage: cscript //nologo " 'VBS
s = s & WScript.ScriptName 'VBS
s = s & " fileName [NumberOfLinesFromEnd]" 'VBS
WScript.Echo s 'VBS
WScript.Quit 'VBS
End If 'VBS
'
' Get the arguments, fileName and tailLines
fileName=WScript.Arguments.Unnamed(0) 'VBS
If WScript.Arguments.Count > 1 Then 'VBS
tailLines = CLng(WScript.Arguments.Unnamed(1)) 'VBS
Else 'VBS
tailLines = 10 'VBS
End If 'VBS
'
' Check that the file exists
Set FSO=CreateObject("Scripting.FileSystemObject") 'VBS
if not FSO.FileExists(fileName) Then 'VBS
WScript.Echo "File " & fileName & " Not Found" 'VBS
WScript.Quit 'VBS
End If 'VBS
'
' Calculate the number of lines
Set f = FSO.OpenTextFile(fileName, ForReading, True) 'VBS
n = 0 'VBS
Do While Not f.AtEndOfStream 'VBS
n = n + 1 'VBS
s = f.ReadLine 'VBS
Loop 'VBS
'
' Output the tail part of the file
Set f = FSO.OpenTextFile(fileName, ForReading, True) 'VBS
i = 0 'VBS
Do While Not f.AtEndOfStream 'VBS
i = i + 1 'VBS
s = f.ReadLine 'VBS
If i > n - tailLines Then WScript.Echo s 'VBS
Loop 'VBS

The output will be
C:\_D\TEMP>cmdfaq
This is line 10
This is line 11
This is line 12

All the best, Timo

--
Prof. Timo Salmi ftp & http://garbo.uwasa.fi/ archives 193.166.120.5
Department of Accounting and Business Finance ; University of Vaasa
mailto:t...@uwasa.fi <http://www.uwasa.fi/~ts/> ; FIN-65101, Finland
Useful script files and tricks ftp://garbo.uwasa.fi/pc/link/tscmd.zip

William Allen

unread,
May 8, 2005, 3:39:18 AM5/8/05
to
"Timo Salmi" wrote in message

> fpschultze wrote:
> > The batch code below does the same and requires WSH which ships
> > with all flavours of Windows since Windows 98 (or SE). :: Create a
> > temp. VBS file that shows the last 10 lines of a text file
>
> > ECHO content = Split(file.ReadAll, vbCrLf, -1, 1)>>%TEMP%.\_.VBS
>
> Scripting Runtime Library documentation: "For large files,
> using the ReadAll method wastes memory resources. Other techniques
> should be used to input a file, such as reading a file line by line."
>
> Here is an alternative, written for XP
...snip

> Do While Not f.AtEndOfStream 'VBS
...snip

> Do While Not f.AtEndOfStream 'VBS

Assuming that the file consists of a large number of lines, but
that the number of tail lines to be output is relatively small, it's
more efficient to read the file once only. During the read, instead
of counting the lines, you push each line into a small, rotating
stack held in a (size=TailLines) dynamic array.

At the EndOfStream, you pop the stack array to echo the tail.

Leaving out the checking routines for arguments/file-existence,
this fragment of VBS code shows the rotating stack algorithm
(assumes filename=first argument TailLines=second argument):

Lines that don't begin with two spaces have wrapped accidentally
====Begin cut-and-paste (omit this line)
Set Args=WScript.Arguments
fileName=Args(0):tail=Args(1)
Set fso=CreateObject("Scripting.FileSystemObject")
Set f=fso.OpenTextFile(fileName, 1)
n=0:ReDim line(tail)
Do While Not f.AtEndOfStream
n=(n+1) Mod tail
line(n)=f.ReadLine
Loop
For j=1 To tail
n=(n+1) Mod tail
wscript.echo line(n)
Next

====End cut-and-paste (omit this line)
For study/demo use. Cut-and-paste as VBScript .VBS plain-text file.
Batch file troubleshooting: http://www.allenware.com/find?UsualSuspects

============Screen capture Windows 95
C:\WORK>type file.txt
one
two
three
four
five
six
seven
eight
nine
ten

C:\WORK>cscript//nologo demo.vbs file.txt 4
seven
eight
nine
ten

C:\WORK>
============End screen capture

--
William Allen
Free interactive Batch Course http://www.allenware.com/icsw/icswidx.htm
Batch Reference with examples http://www.allenware.com/icsw/icswref.htm
Header email is rarely checked. Contact us at http://www.allenware.com/


Todd Vargo

unread,
May 8, 2005, 8:04:45 PM5/8/05
to

"William Allen" wrote:
> Leaving out the checking routines for arguments/file-existence,
> this fragment of VBS code shows the rotating stack algorithm
> (assumes filename=first argument TailLines=second argument):
>
> Lines that don't begin with two spaces have wrapped accidentally
> ====Begin cut-and-paste (omit this line)
> Set Args=WScript.Arguments
> fileName=Args(0):tail=Args(1)
> Set fso=CreateObject("Scripting.FileSystemObject")
> Set f=fso.OpenTextFile(fileName, 1)
> n=0:ReDim line(tail)
> Do While Not f.AtEndOfStream
> n=(n+1) Mod tail
> line(n)=f.ReadLine
> Loop
> For j=1 To tail
> n=(n+1) Mod tail
> wscript.echo line(n)
> Next

Nice code, but it needs to verify that 'tail' has been reached rather than
assume the file has more lines then the number provided. The following
condition will satisfy that need.

If Not line(n)=vbEmpty Then wscript.echo line(n)

--
Todd Vargo (double "L" to reply by email)

John Savage

unread,
May 11, 2005, 8:50:01 PM5/11/05
to
"Klaus Meinhard" <K_Mei...@t-online.de> writes:
>I know I am a spoil-sport, but using 4DOS this is, again, a 1-liner.
>
>To see the last 10 lines of a file, use
>
> tail /n10 myfile.txt

It's a one-liner when implemented using SED, too.

set N=10
set FILE=datafile.txt
sed ":b;$q;N;s/\n/&/%N%;ta;bb;:a;D" %FILE%

Not lightning fast, but does the job for returning up to 80 or so lines.

Standard Caveat
Under DOS, users must test every SED script individually to make sure it
is going to work with their particular executable. Every DOS version of
SED I have tried contains one or more bugs which will inevitably disallow
correct operation of at least some scripts. This TAIL script executes
fine with sed302 but not with sed118 or sed15.
--
John Savage (my news address is not valid for email)

Dr John Stockton

unread,
May 12, 2005, 2:49:10 PM5/12/05
to
JRS: In article <050512000104613.12May05$rook...@suburbian.com>,
dated Thu, 12 May 2005 00:50:01, seen in news:alt.msdos.batch, John
Savage <rook...@suburbian.com.au> posted :

>
>Standard Caveat
>Under DOS, users must test every SED script individually to make sure it
>is going to work with their particular executable. Every DOS version of
>SED I have tried contains one or more bugs which will inevitably disallow
>correct operation of at least some scripts. This TAIL script executes
>fine with sed302 but not with sed118 or sed15.

Then use MiniTrue, which has had a single author and a single line of
development. HEAD is Example 6 in the Help, and TAIL is an appendage to
HEAD.

It's more convenient for file editing that SED is, and also does stream
editing and file viewing (Hex optional).

ISTR that it's in ftp://garbo.uwasa.fi/pc/fileutil/ as well.

--
© John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 MIME. ©
Web <URL:http://www.merlyn.demon.co.uk/> - FAQish topics, acronyms, & links.
I find MiniTrue useful for viewing/searching/altering files, at a DOS prompt;
free, DOS/Win/UNIX, <URL:http://www.idiotsdelight.net/minitrue/> Update hope?

gaurav...@gmail.com

unread,
Apr 1, 2014, 11:16:26 PM4/1/14
to
Hi William Allen,

i am new to window batch file,can you please help me regarding window batch file.i have combined .bat file and .zip file together in one batch file, now i want to extract zip from batch file.can it is possible without using third party tool???

Todd Vargo

unread,
Apr 2, 2014, 10:39:37 PM4/2/14
to
gauravbgpt88, too many wrongs won't make a right.

1) You responded to a post from May 8, 2005.
2) Your post has nothing to do with the subject that you responded to.
3) William Allen has not posted to this group for several years.
4) You are asking for help but provide no useful details to spoon-feed.

Sorry it did not work out.

--
Todd Vargo
(Post questions to group only. Remove "z" to email personal messages)

Stanley Daniel de Liver

unread,
Apr 3, 2014, 2:15:26 PM4/3/14
to
On Thu, 03 Apr 2014 03:39:37 +0100, Todd Vargo <tlv...@sbcglobal.netz>
wrote:
Sadly his domain is for sale. I doubt he'll get any help there.

--
It's a money /life balance.

foxidrive

unread,
Apr 3, 2014, 7:34:51 PM4/3/14
to
>>>> William Allen
>>>> Free interactive Batch Course http://www.allenware.com/icsw/icswidx.htm
>>>> Batch Reference with examples http://www.allenware.com/icsw/icswref.htm
>>>> Header email is rarely checked. Contact us at http://www.allenware.com/

>> 3) William Allen has not posted to this group for several years.
>>
> Sadly his domain is for sale. I doubt he'll get any help there.

I'm often wondered what happened to William. Maybe he passed away.

Does anyone remember any details about him? I had the impression that William was from England but
I'm not sure now.

I looked in google but didn't come up with any leads.

foxi




foxidrive

unread,
Apr 3, 2014, 8:01:03 PM4/3/14
to
>>>> William Allen
>>>> Free interactive Batch Course http://www.allenware.com/icsw/icswidx.htm
>>>> Batch Reference with examples http://www.allenware.com/icsw/icswref.htm
>>>> Header email is rarely checked. Contact us at http://www.allenware.com/

>> 3) William Allen has not posted to this group for several years.
>>
> Sadly his domain is for sale. I doubt he'll get any help there.

I'm often wondered what happened to William. Maybe he passed away.

Does anyone remember any details about him? I had the impression that William was from England but
I'm not sure now.

I looked in google but didn't come up with any leads.

foxi


Well it would help if I spelled his name correctly. d'oh!

This website is still live
http://allenware.co.uk/

twitter was used for a short while - it ended in May 2011 from what I can tell.
https://twitter.com/allenware


This message shows that William stopped using twitter within days of when he last posted to
alt.msdos.batch
https://groups.google.com/forum/#!topic/alt.msdos.batch/KfLnWutpKq8



0 new messages