#16701 - 2002-01-27 06:25 PM
Extract a portion of a log.txt file to a new (Here is a sample)
|
synwave7
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
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
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
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
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
|
|
|
|
#16711 - 2002-01-28 02:09 AM
Re: Extract a portion of a log.txt file to a new (Here is a sample)
|
synwave7
Fresh Scripter
Registered: 2002-01-27
Posts: 49
Loc: Chicago, IL. 60601
|
les_ligetfalvy, I triedcode 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
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
|
|
|
|
#16714 - 2002-01-29 01:15 AM
Re: Extract a portion of a log.txt file to a new (Here is a sample)
|
synwave7
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
|
|
|
|
#16719 - 2002-02-03 02:52 AM
Re: Extract a portion of a log.txt file to a new (Here is a sample)
|
Les
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
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
|
|
|
|
Moderator: Jochen, Allen, Radimus, Glenn Barnas, ShaneEP, Ruud van Velsen, Arend_, Mart
|
0 registered
and 583 anonymous users online.
|
|
|