Page 1 of 2 12>
Topic Options
#16701 - 2002-01-27 06:25 PM Extract a portion of a log.txt file to a new (Here is a sample)
synwave7 Offline
Fresh Scripter

Registered: 2002-01-27
Posts: 49
Loc: Chicago, IL. 60601
Here is a sample of the log.txt file I want to extract from. Where 20020126 is the date in yyyy/mm/dd format which of course changes every day. I suppose I would key in on the beginning of each line to tell the script what portion of the log to xtract to a new file.

20020126 050633 [CAT] 5/21/00 9:41 AM [ID:80D7,SESSION:2] is merged.(files=105)
20020126 050633 [CAT] 5/21/00 9:41 AM [ID:80D7,SESSION:3] is merged.(files=1)
20020126 050633 [CAT] 5/21/00 9:41 AM [ID:80D7,SESSION:4] is merged.(files=1)
20020126 050633 [CAT] 5/21/00 9:41 AM [ID:80D7,SESSION:5] is merged.(files=0)
20020126 050634 [CAT] 5/21/00 9:41 AM [ID:80D7,SESSION:6] is merged.(files=1)
20020126 050634 [CAT] 5/21/00 9:41 AM [ID:80D7,SESSION:7] is merged.(files=0)

_________________________
Bang that head that doesn't bang!!!

Top
#16702 - 2002-01-27 06:54 PM Re: Extract a portion of a log.txt file to a new (Here is a sample)
Les Offline
KiX Master
*****

Registered: 2001-06-11
Posts: 12734
Loc: fortfrances.on.ca
synwave7,
I tossed together the basics of reading and writing. If, however you want to remove those lines from log.txt and keep log.txt, that is another matter.
code:

Break on
$CRLF=CHR(13)+CHR(10)

If Exist("c:\kix4.02\newlog.txt")
Del "c:\kix4.02\newlog.txt"
EndIf

If Open(1,"c:\kix4.02\newlog.txt",5) <> 0
? "Newlog file not opened, error code: [" + @ERROR + "]"
GoTo skip
EndIf

If Open(2,"c:\kix4.02\log.txt") = 0
$line = ReadLine(2)
WHILE @ERROR = 0
? "Line read: [" + $line + "]"
If instr($line, "20020126") <> 0
WriteLine(1,$Line+$CRLF)
Endif
$line = ReadLine(2)
LOOP
Close (2)
ELSE
BEEP
? "Log file not opened, error code: [" + @ERROR + "]"
EndIf


:skip
Close (1)

get $_


_________________________
Give a man a fish and he will be back for more. Slap him with a fish and he will go away forever.

Top
#16703 - 2002-01-27 07:44 PM Re: Extract a portion of a log.txt file to a new (Here is a sample)
synwave7 Offline
Fresh Scripter

Registered: 2002-01-27
Posts: 49
Loc: Chicago, IL. 60601
les_ligetfalvy,
Sorry for the xtra post. I will try to get it right when posting or replying.....I will try to clarify. I do indeed need to keep log.txt intact. What I need overall is to keep log.txt intact while copying some of the lines out of the file and paste those lines into a new file called somename, leaving log.txt where it is and intact, as tho I never touched it, and save the new file to somedirectory. I have been trying to understand your code, I'm new to this. Some questions if I may....Why Carriage Return Line feed to start? I'm not following the If Open 1, error code then If Open 2, start making the new file? Am I close? Can you explain a little what the code does. I really am trying here, I have been reading up on Kix and get the basics here like the If Open line Open (filenumber, "filename", mode) I don't see a nmuber 5 for mode in the book Kix2001, what does it stand for?

Thanks a million!
Chris

_________________________
Bang that head that doesn't bang!!!

Top
#16704 - 2002-01-27 08:02 PM Re: Extract a portion of a log.txt file to a new (Here is a sample)
Les Offline
KiX Master
*****

Registered: 2001-06-11
Posts: 12734
Loc: fortfrances.on.ca
Chris,
No prob... I'll try and explain...

First, the $CRLF variable is simply created to be used later in WriteLine(). It's not necessary to make it a variable, just a common practice. I could just as easily have used "WriteLine(1,$Line+CHR(13)+CHR(10))"

The If Open() statements are just standard error trapping. They are not nested but branched, as there is no point in proceeding if the first Open() fails.

You need two Open() statements, one for the Newlog in "create/write" mode (hence the "5"), and the second for log.txt in "read" (hence no 3rd par). The "5" is really 1 (If the file does not exist, OPEN will create a new file) + 4 (Opens the file for write access).

[ 27 January 2002: Message edited by: LLigetfa ]

_________________________
Give a man a fish and he will be back for more. Slap him with a fish and he will go away forever.

Top
#16705 - 2002-01-27 08:40 PM Re: Extract a portion of a log.txt file to a new (Here is a sample)
kholm Offline
Korg Regular
*****

Registered: 2000-06-19
Posts: 714
Loc: Randers, Denmark
Chris,

An easy way of extracting todays log-entries would be to shell out and use the Find command
to look for date on the format YYYYMMDD and pipe the result to 'todays' logfile
(This can be done because each log entry always contains the date)

Example:

code:

$LogFile = "c:\program files\somedir\log.txt"
$YYYYMMDD = Left(@Date, 4) + SubStr(@Date, 5, 2) + Right(@Date, 2)
$TodayLog = "c:\program files\somedir\" + $YYYYMMDD + ".txt"
Shell '%COMSPEC% /C Type "$LogFile" | Find "$YYYYMMDD" > "$TodayLog"'

In order to retrieve yesterdays log you have to do some date calculations. If you use Kix version 4
you can do that using the UDF SerialDate()

Example:

code:

$TodayInt = SerialDate(@Date) ; Convert date to number
$YesterdayInt = $TodayInt - 1 ; Subtract 1 from date = yesterday
$DateSep = SerialDate($YesterdayInt) ; Convert to date on format: YYYY/MM/DD
$YYYYMMDD = Left($DateSep, 4) + SubStr($DateSep, 5, 2) + Right($DateSep, 2)
$LogFile = "c:\program files\somedir\log.txt"
$DayLog = "c:\program files\somedir\" + $YYYYMMDD + ".txt"
Shell '%COMSPEC% /C Type "$LogFile" | Find "$YYYYMMDD" > "$DayLog"'
Return

function SerialDate($ExpD)
; Author: ScriptLogic Corporation
; parameter ($ExpD) must be a date (in the form of yyyy/mm/dd)
; or an integer previously derived by this function.
; if passed a date, it returns an integer.
; If passed an integer, it returns the date.
; Integers can be used for general-purpose math computations on dates
; and then converted back to dates by calling this function again.
; Algorithms were obtained from: http://www.capecod.net/~pbaum/date/date0.htm
dim $z,$h,$a,$b,$c,$y,$m,$d
if instr($ExpD,'/') ; date passed
$y=val(substr($ExpD,1,4))
$m=val(substr($ExpD,6,2))
$d=val(substr($ExpD,9,2))
if $m<3
$m=$m+12
$y=$y-1
endif
; return an integer
$SerialDate=$d+(153*$m-457)/5+365*$y+$y/4-$y/100+$y/400-306
else ; integer passed
$z=0+$ExpD+306 ; force numeric
$h=100*$z-25
$a=$h/3652425
$b=$a-$a/4
$y=(100*$b+$h)/36525
$c=$b+$z-365*$y-$y/4
$m=(5*$c+456)/153
$d=$c-(153*$m-457)/5
if $m>12
$y=$y+1
$m=$m-12
endif
if $m<=9
$m='0'+$m
endif
if $d<=9
$d='0'+$d
endif
; return a string date
$SerialDate=''+$y+'/'+$m+'/'+$d
endif
endfunction

-Erik

[ 27 January 2002: Message edited by: kholm ]

Top
#16706 - 2002-01-27 09:15 PM Re: Extract a portion of a log.txt file to a new (Here is a sample)
synwave7 Offline
Fresh Scripter

Registered: 2002-01-27
Posts: 49
Loc: Chicago, IL. 60601
Erik,
Thanks for the response, man my brain is smoked already. I clicked on the COM URL that LLgetfa gave me and I have been trying to sort all this out. Thanks to both of you for all the help. If I am reading your repsonse correctly you are assuming the file names I am using are somehow date related? ie. 20020126. The file names are not date related in any way. All I am trying to do is automate opening my log file, copying some of the lines out of it, in this case the lines related to the previous days entries into the file, and paste them into another file. I had no idea there were issues with the size of the file and all that. I guess parsing a 25MB text file for certain lines of text then copying those lines and pasting them into a new file is more difficult then I had anticipated. I am going to keep working at it though. Has anyone out there already accomplished code for this? I am slowly learning but I have never been very good at code which I am now going to try and conquer as well. Anyone?
_________________________
Bang that head that doesn't bang!!!

Top
#16707 - 2002-01-27 09:20 PM Re: Extract a portion of a log.txt file to a new (Here is a sample)
synwave7 Offline
Fresh Scripter

Registered: 2002-01-27
Posts: 49
Loc: Chicago, IL. 60601
LLigetfa,
I have been playing around with the code you sent and I can't even get this simple code to work...shows how scriptwriting deficient I am huh. Anyway, I have created c:\kix4.02\log.txt which I am assuming is my existing log file, which contains the lines with the heading, or first charachters of 20020126. I rem'd out the delete stuff and also created c:\kix4.02\newlog.txt. When I run the script I get the beep and get sent to skip: and the script ends. I'm not sure what I am doing wrong?
_________________________
Bang that head that doesn't bang!!!

Top
#16708 - 2002-01-27 11:08 PM Re: Extract a portion of a log.txt file to a new (Here is a sample)
Les Offline
KiX Master
*****

Registered: 2001-06-11
Posts: 12734
Loc: fortfrances.on.ca
Chris,
Not sure what's going on. I tested the script on Win2K using KiX 4.02 and 3.62 without any prob. Are you sure you copy/pasted it correctly? The best way to copy paste it if you lose the formatting, is to first paste it into M$ Word and then cut/paste it from Word to your editor.
_________________________
Give a man a fish and he will be back for more. Slap him with a fish and he will go away forever.

Top
#16709 - 2002-01-27 11:18 PM Re: Extract a portion of a log.txt file to a new (Here is a sample)
Les Offline
KiX Master
*****

Registered: 2001-06-11
Posts: 12734
Loc: fortfrances.on.ca
Chris,
Obviously, if it beeps, then KiX has a problem with the code, 'cause I don't have any beep command in my sample. Spark up a DOS box and run the script for there. Whatever error KiX reports will stay in the DOS box. You can also run it with the "/d" switch and single step through it.
quote:

Microsoft Windows 2000 [Version 5.00.2195]
(C) Copyright 1985-2000 Microsoft Corp.

C:\KiX4.02>C:\KiX4.02\kix32.exe C:\KiX4.02\test.kix /d


_________________________
Give a man a fish and he will be back for more. Slap him with a fish and he will go away forever.

Top
#16710 - 2002-01-28 01:56 AM Re: Extract a portion of a log.txt file to a new (Here is a sample)
synwave7 Offline
Fresh Scripter

Registered: 2002-01-27
Posts: 49
Loc: Chicago, IL. 60601
les_ligetfalvy,
Thank you, thank you! I got it working! It was the directory name that the script didn't like, I believe. What is the rule in kix for long file names or long directory names such as c:\program files\some other long dir name. I can't get the script to run if I lengthen the dir name beyond eight characters? Can you shed some light on this?

Thanks again,
Chris

_________________________
Bang that head that doesn't bang!!!

Top
#16711 - 2002-01-28 02:09 AM Re: Extract a portion of a log.txt file to a new (Here is a sample)
synwave7 Offline
Fresh Scripter

Registered: 2002-01-27
Posts: 49
Loc: Chicago, IL. 60601
les_ligetfalvy,
I tried

code follows:
If Open(1,"c:\Progra~1\Comput~1\ArcServ\ArcServ.log",5) <> 0

endcode:

what about when copying could I use

code follows:

$dest = chr(34) + "c:\Long dir name with spaces\another LDNWS\" + chr(34)
shell "%comspec% /c copy c:\*.txt $dest"

endcode:


what about the If Open command though is the above alright. (the code with the ~1 in the names?

signed,
very confused newbie

_________________________
Bang that head that doesn't bang!!!

Top
#16712 - 2002-01-28 02:22 AM Re: Extract a portion of a log.txt file to a new (Here is a sample)
Les Offline
KiX Master
*****

Registered: 2001-06-11
Posts: 12734
Loc: fortfrances.on.ca
Chris,
Without seeing your code, I can't tell what's going on. KiX certainly can handle long folder and file names. The rules are no different than with Windows. Just so long as they are wrapped in quotes so that it can delineate parms. If you have nested quotes however, you need to use single quotes on the outside and double quotes on the inside. If you are trying to build strings with embedded quotes, you need to use CHR(34).

Just to be certain, Itested the following with both 4.02 and 3.62 and there was no prob.

code:

Break on
$CRLF=CHR(13)+CHR(10)

If Exist("C:\Documents and Settings\Admin\Desktop\new extra long log.txt")
Del "C:\Documents and Settings\Admin\Desktop\new extra long log.txt"
EndIf

If Open(1,"C:\Documents and Settings\Admin\Desktop\new extra long log.txt",5) <> 0
? "Newlog file not opened, error code: [" + @ERROR + "]"
GoTo skip
EndIf

If Open(2,"C:\Documents and Settings\Admin\Desktop\log.txt") = 0
$line = ReadLine(2)
WHILE @ERROR = 0
? "Line read: [" + $line + "]"
If instr($line, "20020126") <> 0
WriteLine(1,$Line+$CRLF)
Endif
$line = ReadLine(2)
LOOP
Close (2)
ELSE
BEEP
? "Log file not opened, error code: [" + @ERROR + "]"
EndIf


:skip
Close (1)

get $_


Look over your code carefully and make sure you don't have an odd number of quotes or an if without an endif etc. Download KiXStripxxx from http://home.wanadoo.nl/scripting/ and use it to check your code.

[ 28 January 2002: Message edited by: LLigetfa ]

_________________________
Give a man a fish and he will be back for more. Slap him with a fish and he will go away forever.

Top
#16713 - 2002-01-28 04:41 AM Re: Extract a portion of a log.txt file to a new (Here is a sample)
synwave7 Offline
Fresh Scripter

Registered: 2002-01-27
Posts: 49
Loc: Chicago, IL. 60601
Les,
Quite right my friend! Amatuer jumping the gun. People like you make this job worth while. Everything is working great! I hope I can help someone else like you have helped me....someday.

Best regards,
Chris

_________________________
Bang that head that doesn't bang!!!

Top
#16714 - 2002-01-29 01:15 AM Re: Extract a portion of a log.txt file to a new (Here is a sample)
synwave7 Offline
Fresh Scripter

Registered: 2002-01-27
Posts: 49
Loc: Chicago, IL. 60601
OK, so I am becoming dangerous with this kix32 stuff. My first code is working but I need to tweak it a little. Is there a way to make the Jan-26 change each day to the current days date? I need to run this code each day and would hate to have to open the code up every day to change this date. The code I have written simply parses the existing log file, which is a text file, for the lines that begin with Jan-26 (current date at time of code creation needs to be current date every day) and copy those lines to a new text file so that I can have each days input into the ongoing log, on avg about 50 lines, put into another file leaving the ongoing log intact for further use.

If Open(2,"c:\logs\currentlog.txt") = 0
$line = ReadLine(2)
WHILE @ERROR = 0
? "Line read: [" + $line + "]"
If instr($line, "Jan-26") <> 0
WriteLine(1,$Line+$CRLF)
Endif
$line = ReadLine(2)
LOOP
Close (2)
ELSE
BEEP ? "Log file not opened, error code: [" + @ERROR + "]"
EndIf

Chris

_________________________
Bang that head that doesn't bang!!!

Top
#16715 - 2002-01-29 02:12 AM Re: Extract a portion of a log.txt file to a new (Here is a sample)
Les Offline
KiX Master
*****

Registered: 2001-06-11
Posts: 12734
Loc: fortfrances.on.ca
Chris,
Are you changing the rules on us? In your first example the date was in the format YYYYMMDD which is the same as @date. Now we need to know what all 12 month abreviations are and whether the day numbers < 10 are one or two digits.

KiX has macros that can return the full month name @Month and month day number @MDayNo. You could build an array of abreviations and use @MonthNo to pull the current one.

_________________________
Give a man a fish and he will be back for more. Slap him with a fish and he will go away forever.

Top
#16716 - 2002-02-02 06:28 PM Re: Extract a portion of a log.txt file to a new (Here is a sample)
synwave7 Offline
Fresh Scripter

Registered: 2002-01-27
Posts: 49
Loc: Chicago, IL. 60601
Les,
Sorry for any confusion. I only work Sat, Sun and Monday so I am only now getting back to this. This script will be running in 2 environments, Novell which logs date format as Feb-02 and NT/2000 which logs date format as 20020202. Is there code you can refer me to which is already written to search for a changing date format. IOW the script needs to change what date it's looking for everyday.

Thanks,
Chris

_________________________
Bang that head that doesn't bang!!!

Top
#16717 - 2002-02-02 09:05 PM Re: Extract a portion of a log.txt file to a new (Here is a sample)
Les Offline
KiX Master
*****

Registered: 2001-06-11
Posts: 12734
Loc: fortfrances.on.ca
Don't know of any off-the-rack code, but it would be no big deal to toss something together. I basically outlined what you need to do. If you need some help with the code, no prob, however, as I said "we need to know what all 12 month abreviations are and whether the day numbers < 10 are one or two digits".
I can assume that by your reply "logs date format as Feb-02", the numbers are always two digits, but I still don't know what the twelve month abbreviations are. Supply those and we'll be good to go.
I also need to know if both formats need be searched from within the same script, or if they will be separate.
_________________________
Give a man a fish and he will be back for more. Slap him with a fish and he will go away forever.

Top
#16718 - 2002-02-03 01:52 AM Re: Extract a portion of a log.txt file to a new (Here is a sample)
synwave7 Offline
Fresh Scripter

Registered: 2002-01-27
Posts: 49
Loc: Chicago, IL. 60601
Les,
I could really use some help with the code for this. As far as what's needed, day numbers < 10 are indeed 2 numbers. And yes they are 2 different scripts. So, the format for the one script should search on the 3 letter-2 digit format IOW Feb-02, Mar-02, Apr-02 Apr-10, Apr-11 and so on. The format for the other script should search on the yyyymmdd format. If you could throw something together that would be awsome!

Thanks,
Chris

_________________________
Bang that head that doesn't bang!!!

Top
#16719 - 2002-02-03 02:52 AM Re: Extract a portion of a log.txt file to a new (Here is a sample)
Les Offline
KiX Master
*****

Registered: 2001-06-11
Posts: 12734
Loc: fortfrances.on.ca
Well, assuming I got the 3 char months right, this bit of code should do it.

$MDayNo = Val(@MDayNo)
If $MDayNo < 10
$MDayNo = "0" + $MDayNo
EndIf

$MonthNo = Val(@MonthNo)
$AllMonths = "JanFebMarAprMayJunJulAugSepOctNovDec"
$Search = SubStr($AllMonths,($MonthNo * 3 - 2),3) + "-" + $MDayNo

? "$$MDayNo = " + $MDayNo
? "$$MonthNo = " + $MonthNo
? "$$Search = " + $Search

For the YYYYMMDD format

$Date = @Date
$Sdate = (Substr($Date,1,4)+Substr($Date,6,2)+Substr($Date,9,2))
$Search = $Sdate

Then all you need do is substitute the hard coded search criteria with $Search and you're good to go.

If instr($line, $Search) <> 0

This of course is only if you run the script on the day the log entry was created. If you run it on the following day then you'll need to do date math as Erik suggested.

_________________________
Give a man a fish and he will be back for more. Slap him with a fish and he will go away forever.

Top
#16720 - 2002-02-03 03:57 AM Re: Extract a portion of a log.txt file to a new (Here is a sample)
synwave7 Offline
Fresh Scripter

Registered: 2002-01-27
Posts: 49
Loc: Chicago, IL. 60601
Les,
Can't thank you enough. My shift is about over so I'm gonna tool around with this for a few more minutes and try to implement it tomorrow. Thanks again.

p.s. I'm learning little by little.

Tak or
Dank je wel?,
Chris

_________________________
Bang that head that doesn't bang!!!

Top
Page 1 of 2 12>


Moderator:  Jochen, Allen, Radimus, Glenn Barnas, ShaneEP, Ruud van Velsen, Arend_, Mart 
Hop to:
Shout Box

Who's Online
0 registered and 661 anonymous users online.
Newest Members
Viginette, ManuvdWielNL, Sir_Barrington, batdk82, StuTheCoder
17888 Registered Users

Generated in 0.074 seconds in which 0.024 seconds were spent on a total of 13 queries. Zlib compression enabled.

Search the board with:
superb Board Search
or try with google:
Google
Web kixtart.org