Last 20 Lines of a Log

Technical support and scripting issues

Moderators: JRL, Dorian (MJT support)

Post Reply
armsys
Automation Wizard
Posts: 1108
Joined: Wed Dec 04, 2002 10:28 am
Location: Hong Kong

Last 20 Lines of a Log

Post by armsys » Fri Feb 25, 2005 12:03 pm

I produce a backup log (a text file) by DateStamp>. How can I create a text file containing only the last 20 lines from the backup log?

Creating a string containing the entire backup log is simple:
ReadFile>Backup.log,File

The challenge is how to extract the last 20 lines from the string varialbe.

User avatar
support
Automation Wizard
Posts: 1450
Joined: Sat Oct 19, 2002 4:38 pm
Location: London
Contact:

Post by support » Fri Feb 25, 2005 12:59 pm

How about this:

Let>LogF=d:\test.log
Let>OutFile=d:\new.log

VBSTART
Function NumLines(Filename)
Set fso = CreateObject("Scripting.FileSystemObject")
Set f = fso.OpenTextFile(Filename, 1)
f.ReadAll
NumLines = f.Line
End Function
VBEND

VBEval>NumLines("%LogF%"),Lines
Let>thisline=Lines-20
If>thisline>0
Repeat>thisline
ReadLn>LogF,thisline,theline
WriteLn>OutFile,r,theline
Let>thisline=thisline+1
Until>thisline,Lines
else
MessageModal>Not enough lines
endif
MJT Net Support
[email protected]

armsys
Automation Wizard
Posts: 1108
Joined: Wed Dec 04, 2002 10:28 am
Location: Hong Kong

Post by armsys » Fri Feb 25, 2005 1:22 pm

Hi Support,

I'm grateful to you for personally writing a sample VBscript which I need desparately. Your enlightening script highlights that VBscript begins where Macro Scheduler stops.

The idea behind the last 20-line log originates from the needs to send me various logs remotely from customers' file serves using the SMTPSendMail> command periodically. As time elapses for weeks, the size of the logs becomes enormous. All I'm interested is the file server status during the last 24 hours.

Thanks a lot for your generous help.

User avatar
support
Automation Wizard
Posts: 1450
Joined: Sat Oct 19, 2002 4:38 pm
Location: London
Contact:

Post by support » Fri Feb 25, 2005 1:30 pm

You could do the same thing in MacroScript using ReadFile by counting the number of CRLF characters to determine the number of lines. Or you could use ReadLn in a first parse loop which just counts the number of lines until you reach EOF, but this would be slower. I just thought the VBScript method was cleaner.
MJT Net Support
[email protected]

Me_again
Automation Wizard
Posts: 1101
Joined: Fri Jan 07, 2005 5:55 pm
Location: Somewhere else on the planet

Post by Me_again » Fri Feb 25, 2005 4:14 pm

If the file is large and the lines are all the same length maybe it would be faster to calculate the total lines from the size?

adroege
Automation Wizard
Posts: 438
Joined: Tue Dec 07, 2004 7:39 pm

Tail a file very quickly

Post by adroege » Fri May 28, 2010 3:41 pm

Based upon some more advanced techniques I found here in the forum, here is some sample code which should be lightning fast even on huge files. I post it here to help any users which found this thread by searching. There is of course the tail command on Linux - and also a windows port of this program... however this is an efficient implementation in Macro Scheduler and VBScript.

[code]
VBSTART
Function LineCount(sFilespec)
Const ForAppending = 8
with CreateObject("Scripting.FileSystemObject")
LineCount = .OpenTextFile(sFilespec, ForAppending).Line
end with
end Function
VBEND

Let>filename=c:\cm1.txt

VBEval>LineCount("%filename%"),last_line_num

Let>line_num=%last_line_num%-20
Let>Tail_doc=
Repeat>line_num
ReadLn>%filename%,line_num,line
//This is just for safety sake...
If>line=##EOF##,finish
ConCat>%Tail_doc%,%CRLF%
ConCat>%Tail_doc%,line
Add>line_num,1
Until>line_num=last_line_num
Label>finish
MessageModal>%Tail_doc%


[/code]

User avatar
jpuziano
Automation Wizard
Posts: 1085
Joined: Sat Oct 30, 2004 12:00 am

Post by jpuziano » Fri May 28, 2010 5:07 pm

Thanks for that adroege.

For anyone counting lines in truly huge files where speed is critical, the following two posts may be helpful:

Acquire line count of text file
http://www.mjtnet.com/forum/viewtopic.php?t=3069

[Bounty Won] Text Blob Line Counter Speed Challenge
http://www.mjtnet.com/forum/viewtopic.php?t=5408

Take care
jpuziano

Note: If anyone else on the planet would find the following useful...
[Open] PlayWav command that plays from embedded script data
...then please add your thoughts/support at the above post - :-)

Post Reply
Sign up to our newsletter for free automation tips, tricks & discounts