Page 1 of 1 1
Topic Options
#194283 - 2009-06-16 04:15 PM Parse numbers from a text-file and add them together?
Finnerup Offline
Fresh Scripter

Registered: 2008-01-15
Posts: 5
Our mailsystem keeps basic track of users that send attachments to others as our bandwidth is rather expensive (satellite). It informs us by sending a text-based email to us, which we can retrieve and save as a normal .txt-file.

This is how our resulting text-file looks like:

From: SENDER
Sent: 16. juni 2009 14:29
To: RECEIVER
Subject:



[Our Ref:XXXXXXX]

Follow Up Flag: Follow up
Flag Status: Completed

Categories: mlca

REFN: XXXXXXXX INIT:


2009/06/16 09:52:16 COMPUTERNAME User selected to send the mail Ref No: XXX.XXXX
2009/06/16 09:52:16 With Subject: SUBJECTNAME
2009/06/16 09:52:16 No Files: 1 Total size compressed 1617 KB
2009/06/16 09:52:16 * DOCUMENTNAME.pdf-1961KB
From: SENDER
Sent: 16. juni 2009 12:15
To: RECEIVER
Subject:



[Our Ref:XXXXXXX]

Follow Up Flag: Follow up
Flag Status: Completed

Categories: mlca

REFN: XXXXXXXX INIT:


2009/06/16 07:39:06 COMPUTERNAME User selected to send the mail Ref No: XXX.XXXX
2009/06/16 07:39:06 With Subject: SUBJECTNAME
2009/06/16 07:39:06 No Files: 1 Total size compressed 648 KB
2009/06/16 07:39:06 * DOCUMENTNAME.pdf-757KB


What I would like to do is to create a script which can parse this text-file and add all the numbers where it says "Total size compressed XXX KB", but I'm not sure how to get Kix to ignore the rest of the text and only focus on the KB figure.

I'm thinking about something along the lines of using TRIM, but as the position of the value can change, I'm not sure this will work out in the end.

Can anyone point me in the right direction, here?

Thanks in advance!

/Kenneth

Top
#194284 - 2009-06-16 04:24 PM Re: Parse numbers from a text-file and add them together? [Re: Finnerup]
eriqjaffe Offline
Hey THIS is FUN

Registered: 2004-06-24
Posts: 214
Loc: Arlington Heights, IL USA
You could use the OPEN and READLINE commands to find the line that contains "Total size compressed" and then split that line on spaces. Assuming that the text file always formats the lines the same (even if the lines aren't always in the same place), that would give you an array with the number being the second from the last element. You could then push those array elements into another array or just keep a running total.

Pseudo-code:

 Code:
$totalkb = 0

if OPEN(1,"textfile.txt") = 0
   $x = ReadLine(1)
   While @ERROR = 0
      if instr ($x,"Total size compressed")
         $tmp = split($x," ")
         $totalkb = $totalkb + val($tmp[ubound($tmp)-1])
      endif
      $x = ReadLine(1)
   Loop
endif


Edited by eriqjaffe (2009-06-16 04:27 PM)

Top
#194315 - 2009-06-18 02:19 PM Re: Parse numbers from a text-file and add them together? [Re: eriqjaffe]
Finnerup Offline
Fresh Scripter

Registered: 2008-01-15
Posts: 5
Thanks a bunch!

That gave a good direction.

Furthermore in the same connection, I have to parse a bunch of data as below, looking for data sizes and add it up all the KB figures.

Any good ideas on how to do that with a random blob of data?

 Code:
2009/06/03 12:28:17 COMPUTERNAME Mail Files over 10MB Listing files:
2009/06/03 12:28:17  * FILENAME1.zip-1523KB * FILENAME2.zip-1435KB * FILENAME3.zip-1325KB * FILENAME4.zip-1229
KB * FILENAME5.zip-1379KB * FILENAME6.zip-829KB * FILENAME7.zip-1046KB * FILENAME8.zip-1137KB * FILENAME9.zip-1481KB * FILENAME10.zip-1554KB * FILENAME11.zip-1339KB


/Kenneth

Top
#194316 - 2009-06-18 02:33 PM Re: Parse numbers from a text-file and add them together? [Re: Finnerup]
Mart Moderator Offline
KiX Supporter
*****

Registered: 2002-03-27
Posts: 4673
Loc: The Netherlands
Each file size is preceded by a - I see so you could do a split on the - character making it an array, loop through all parts and check if KB is in that specific part, Use the Left() or Substr() function to get rid of the KB and add the numbers.

Edited by Mart (2009-06-18 02:34 PM)
_________________________
Mart

- Chuck Norris once sold ebay to ebay on ebay.

Top
#194320 - 2009-06-19 09:13 AM Re: Parse numbers from a text-file and add them together? [Re: Mart]
Richard H. Administrator Offline
Administrator
*****

Registered: 2000-01-24
Posts: 4946
Loc: Leatherhead, Surrey, UK
Something like this:
 Code:
$sData="2009/06/03 12:28:17  * FILENAME1.zip-1523KB * FILENAME2.zip-1435KB * FILENAME3.zip-1325KB * FILENAME4.zip-1229
KB * FILENAME5.zip-1379KB * FILENAME6.zip-829KB * FILENAME7.zip-1046KB * FILENAME8.zip-1137KB * FILENAME9.zip-1481KB * FILENAME10.zip-1554KB * FILENAME11.zip-1339KB"


$dTotal=0.0
For Each $sField in Split($sData)
	If InStr($sField,".zip-")
		$sFile=Split($sField,"-")[0]
		$dSize=CDbl(Left(Split($sField,"-")[1],-2))
		$sScale=Right(Split($sField,"-")[1],2)
		"File '"+$sFile+"' has size "+$dSize+" "+$sScale@CRLF
		
		; If the input contains a mix of KB, MB, GB then you will need to scale it
		; I'm scaling to the smallest unit (byte) so that no information is lost
		Select
		Case $sScale="KB"
			$dTotal=$dTotal+$dSize*1024
		Case $sScale="MB"
			$dTotal=$dTotal+$dSize*1024*1024
		Case $sScale="GB"
			$dTotal=$dTotal+$dSize*1024*1024*1024
		Case $sScale="TB"
			$dTotal=$dTotal+$dSize*1024*1024*1024*1024
		EndSelect
			
	EndIf
Next

; Display total, converted to MB
"Total size="+FormatNumber($dTotal/1024/1024,2)+" MB"+@CRLF


If you can guarantee that the numbers will always be KB then you can leave out the scaling, but it will work as-is.

Top
Page 1 of 1 1


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

Who's Online
0 registered and 874 anonymous users online.
Newest Members
StuTheCoder, M_Moore, BeeEm, min_seow, Audio
17884 Registered Users

Generated in 0.057 seconds in which 0.027 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