sesk Posted March 20, 2007 Share Posted March 20, 2007 Hello, I am working on a script to handle my server console and manage logs, I got it working ok but the log part isn't functional. I'm looking for a way to get the last 5 lines of a *.log file (5-20mb file) and store them in a common *.log file, with time and date title, like: ---12:30:21 PM - 3/20/2007 --- Last Line -4 Last Line -3 Last Line -2 Last Line -1 Last Line And how to make it so it doesn't procced to the next operation until the lines are read and stored, since when the server starts, it wipes the log files. Thanks for reading. Link to comment Share on other sites More sharing options...
seandisanti Posted March 20, 2007 Share Posted March 20, 2007 Hello, I am working on a script to handle my server console and manage logs, I got it working ok but the log part isn't functional. I'm looking for a way to get the last 5 lines of a *.log file (5-20mb file) and store them in a common *.log file, with time and date title, like: ---12:30:21 PM - 3/20/2007 --- Last Line -4 Last Line -3 Last Line -2 Last Line -1 Last Line And how to make it so it doesn't procced to the next operation until the lines are read and stored, since when the server starts, it wipes the log files. Thanks for reading.you could do a FileReadToArray() then save FileWriteLine() $array[$array[0]-4] through $array[$array[0]] to another file... Link to comment Share on other sites More sharing options...
sesk Posted March 20, 2007 Author Share Posted March 20, 2007 Wouldn't that be... a pretty damn big array ? File Size range is more like 5-200mb; 2,360,000 lines, avg 1million lines, and there would also be to consider the time of execution, isnt there a way to get the file line count, and like GetLastLine(...) do FileLineRead(...,...,lastline) lastline = lastline -1; c = c +1 until c = 5; ? Link to comment Share on other sites More sharing options...
xcal Posted March 20, 2007 Share Posted March 20, 2007 _FileCountLines How To Ask Questions The Smart Way Link to comment Share on other sites More sharing options...
seandisanti Posted March 20, 2007 Share Posted March 20, 2007 Wouldn't that be... a pretty damn big array ? File Size range is more like 5-200mb; 2,360,000 lines, avg 1million lines, and there would also be to consider the time of execution, isnt there a way to get the file line count, and likeGetLastLine(...)do FileLineRead(...,...,lastline) lastline = lastline -1; c = c +1until c = 5;?you're there and could test it for time to execute etc couldn't you? and which is it, 5 - 20 mb, or 5-200? pretty big difference... Link to comment Share on other sites More sharing options...
randallc Posted March 20, 2007 Share Posted March 20, 2007 Hi, If it can be large files, try a "tail"; either use "tail.exe", or my tailRW.au3 (link from sig) as _filecountlines also loads whole file to do the count! (at last count!... anyway) Best, Randall ExcelCOM... AccessCom.. Word2... FileListToArrayNew...SearchMiner... Regexps...SQL...Explorer...Array2D.. _GUIListView...array problem...APITailRW Link to comment Share on other sites More sharing options...
sesk Posted March 20, 2007 Author Share Posted March 20, 2007 (edited) and which is it, 5 - 20 mb, or 5-200? pretty big difference...5-200mb , wont let me edit my original post.Randall,I'll try your tail thing.EDIT: But how do you use it, i tried the ReadTail3.au3 and it only displayed the replacement time in the console :SEDIT2: Nevermind the last line WAS " " (nothing), but now i have to figure out how to make it read the previous lines (btw nice script, real fast ) Edited March 20, 2007 by sesk Link to comment Share on other sites More sharing options...
sesk Posted March 20, 2007 Author Share Posted March 20, 2007 (edited) [x] Edited March 20, 2007 by sesk Link to comment Share on other sites More sharing options...
_Kurt Posted March 20, 2007 Share Posted March 20, 2007 (edited) I think the most logical way to achieve this is to do: #include <File.au3> Local $file = "test.txt", $data = "" $lines = _FileCountLines($file) For $i = $lines-4 To $lines If NOT FileReadLine($file, $i) = "" Then $data = $data & FileReadLine($file, $i) & @CRLF Next MsgBox(0,"",StringTrimRight($data,1)) Kurt EDIT: Improved script. Edited March 20, 2007 by _Kurt Awaiting Diablo III.. Link to comment Share on other sites More sharing options...
flyingboz Posted March 20, 2007 Share Posted March 20, 2007 $lines = _FileCountLines($file)You still are having to read the whole file to get the "tail" end.Find a tail binary, and use it. I have pasted below the info to help you find one. C:\boz\bat>tail --versiontail (textutils) 2.1Written by Paul Rubin, David MacKenzie, Ian Lance Taylor, and Jim Meyering.Copyright © 2002 Free Software Foundation, Inc.This is free software; see the source for copying conditions. There is NOwarranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.A wrapper around tail, and all your problems are solved. Fast access at the 'last' bit of a file. Reading the help file before you post... Not only will it make you look smarter, it will make you smarter. Link to comment Share on other sites More sharing options...
ptrex Posted March 21, 2007 Share Posted March 21, 2007 @All My solution to this problem would be to use the Logparser object. This is what it is designed for. Look at my signature. regards, ptrex Contributions :Firewall Log Analyzer for XP - Creating COM objects without a need of DLL's - UPnP support in AU3Crystal Reports Viewer - PDFCreator in AutoIT - Duplicate File FinderSQLite3 Database functionality - USB Monitoring - Reading Excel using SQLRun Au3 as a Windows Service - File Monitor - Embedded Flash PlayerDynamic Functions - Control Panel Applets - Digital Signing Code - Excel Grid In AutoIT - Constants for Special Folders in WindowsRead data from Any Windows Edit Control - SOAP and Web Services in AutoIT - Barcode Printing Using PS - AU3 on LightTD WebserverMS LogParser SQL Engine in AutoIT - ImageMagick Image Processing - Converter @ Dec - Hex - Bin -Email Address Encoder - MSI Editor - SNMP - MIB ProtocolFinancial Functions UDF - Set ACL Permissions - Syntax HighLighter for AU3ADOR.RecordSet approach - Real OCR - HTTP Disk - PDF Reader Personal Worldclock - MS Indexing Engine - Printing ControlsGuiListView - Navigation (break the 4000 Limit barrier) - Registration Free COM DLL Distribution - Update - WinRM SMART Analysis - COM Object Browser - Excel PivotTable Object - VLC Media Player - Windows LogOnOff Gui -Extract Data from Outlook to Word & Excel - Analyze Event ID 4226 - DotNet Compiler Wrapper - Powershell_COM - New Link to comment Share on other sites More sharing options...
randallc Posted March 21, 2007 Share Posted March 21, 2007 (edited) Hi, [1]tail (textutils) 2.1 A wrapper around tail[2]To get started you need to download it here : MS LogParser 2.2 (Don' t forget to register the LogParser DLL.)Why bother with either of those options when you can use a simple AutoIt UDF [see my post above about TailRW], which is probably faster anyway? Best, randall Edited March 21, 2007 by randallc ExcelCOM... AccessCom.. Word2... FileListToArrayNew...SearchMiner... Regexps...SQL...Explorer...Array2D.. _GUIListView...array problem...APITailRW Link to comment Share on other sites More sharing options...
sesk Posted March 21, 2007 Author Share Posted March 21, 2007 Randallc, I am using your TailRW script, but I get AutoIt error when it runs (Memory Error) local $c_bulk = "Location of bulk txt file" local $c_logs = "Location of server log file (txt, can't read log files...) local $i_line = 0; ... if ... then $i_line = -6; FileWriteLine ($c_bulk, "" ) FileWriteLine ($c_bulk, "--- "& @MDAY &"/"& @MON &"/"& @YEAR &" -- "& @HOUR &":"& @MIN &":"& @SEC &" ---") FileWriteLine ($c_bulk, "" ) do $i_Line = $i_Line + 1 $s_ReadLine = __FileReadLine ($c_logs, $i_Line, 1);** READS from the TAIL if Negative line number FileWriteLine ($c_bulk, $s_ReadLine ) Until $i_Line = -1 Link to comment Share on other sites More sharing options...
randallc Posted March 21, 2007 Share Posted March 21, 2007 Hi, You have not named your file to read? ;ReadLine4.au3 #include "TailRW.au3" $s_Test = FileOpenDialog("Choose File", @ScriptDir, "Files (*.exe;*.dll;*.txt)", 1); ** put your file name HERE Local $c_bulk = "Location of bulk txt file" ;$s_Test must be a file name ****************** Local $c_logs = "Location of server log file (txt, can't read log files...)";$s_Test must be a file name ****************** Local $i_line = 6; $s_ReadLine = __FileReadLine ($s_Test, -$i_line, 1, 1) ;;** READS from the TAIL if Negative line number [to the end if 4th parm=1] ;~ __FileReadLine($s_File, $i_LineNumber = -1, $i_Beginning = 1,$i_ToEnd=0, $i_BufferSize = 8388608) ;~ MsgBox(0, "", "$s_ReadLine=" & $s_ReadLine) FileWrite("AnswerLines.txt",$s_ReadLine) runwait ("notepad "&"AnswerLines.txt")Best, Randall ExcelCOM... AccessCom.. Word2... FileListToArrayNew...SearchMiner... Regexps...SQL...Explorer...Array2D.. _GUIListView...array problem...APITailRW Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now