Subtraction of values

March 11, 2010 at 14:23:33
Specs: Windows 7
I have a text file containing many lines like the following;

SP_BM_BLG,03/03/2010,3:00,22744.000000
SP_BM_BLG,03/03/2010,3:30,22758.000000

What I need to be able to do is run a script that will subtract 22744 from 22758 to end up with the following line in another text file;

SP_BM_BLG,03/03/2010,3:30,14.000000

Is this possible using vbscript as this is what I having been using to work with text files.
Any help as usual is grealy appreciated.


See More: Subtraction of values

Report •

#1
March 11, 2010 at 14:40:33
Are there just 2 lines?

If not, maybe post a meaningful chunk.


=====================================
Helping others achieve escape felicity

M2


Report •

#2
March 11, 2010 at 23:17:35
this would be easier in batchscript, i thinks...
Set fso = createobject("Scripting.FileSystemObject")
public num,n1
Const ForReading = 1
Const ForWriting = 2
Const ForAppending = 8
cnt=0
Set iFile = fso.OpenTextFile ("data.txt", ForReading)
set ofile=fso.createtextfile("data.new",forwriting)
do while iFile.AtEndOfStream=false
x=iFile.readline
if cnt mod 2 = 0 then
getnum
prev=num
else
getnum
y=num-prev
out=n1&y&".000000"
ofile.writeline out
end if
cnt=cnt+1
loop

sub getnum
p=instr(x,":")
if p>0 then
p=instr(p,x,",")
if p>0 then
n1=left(x,p)
num=mid(x,p+1)
end if
end if
end sub

Wscript.Quit


Report •

#3
March 12, 2010 at 04:41:21
There are probably thousands of lines, although I do use a script to extract only the previous days data.

Below is some more lines, the data comes from automatic meter reads taken every half hour. If I take one reading from the other that gives me the half hourly consumption. I presently do this using excel, which is abit time consuming.

Each line has a channelID, Date,Time,Value.

SP_BM_BLG,03/03/2010,03:00,22744
SP_BM_BLG,03/03/2010,03:30,22758
SP_BM_BLG,03/03/2010,04:00,22772
SP_BM_BLG,03/03/2010,04:30,22794
SP_BM_BLG,03/03/2010,05:00,22818
SP_BM_BLG,03/03/2010,05:30,22832
SP_BM_BLG,03/03/2010,06:00,22854
SP_BM_BLG,03/03/2010,06:30,22881
SP_BM_BLG,03/03/2010,07:00,22908
SP_BM_BLG,03/03/2010,07:30,22935
SP_BM_BLG,03/03/2010,08:00,22964

The result from the script should give me the following;

SP_BM_BLG,03/03/2010,03:30,14
SP_BM_BLG,03/03/2010,04:00,14
SP_BM_BLG,03/03/2010,04:30,22
SP_BM_BLG,03/03/2010,05:00,24
SP_BM_BLG,03/03/2010,05:30,14
SP_BM_BLG,03/03/2010,06:00,22
SP_BM_BLG,03/03/2010,06:30,27
SP_BM_BLG,03/03/2010,07:00,27
SP_BM_BLG,03/03/2010,07:30,27
SP_BM_BLG,03/03/2010,08:00,29

Will try the response from nbrane tonight. Thanks


Report •

Related Solutions

#4
March 14, 2010 at 06:07:00
You're looking for what, the amount of increase?

Report •

#5
March 14, 2010 at 13:33:43
Yes, it would be the amount of increase between each line, taking into account the time i.e every half hour.

If you look at the first two lines in the of response 3, the amount of increase from 03:00 to 03:30 is 14 and so on, giving the end results above.

Hope this helps?


Report •

#6
March 15, 2010 at 07:58:42
Ya know nbrain, I think you're over-thinking things too much here. That, or you're not familiar with VBScript:
Const inName  = "in.csv"
Const outName = "out.csv"

Set fso = CreateObject("Scripting.FileSystemObject")
Set outF = fso.OpenTextFile(outName, 2, True)
With fso.OpenTextFile(inName)
  prevNum = CDbl(Split(Trim(.ReadLine), ",")(3))
  Do Until .AtEndOfStream
    line = Split(Trim(.ReadLine), ",")
    curNum = CDbl(line(3))
    line(3) = curNum - prevNum
    prevNum = curNum
    outF.WriteLine Join(line, ",")
  Loop
End With


Report •

#7
March 15, 2010 at 09:09:33
yeah, vbscript is NOT one of my strong points! (hence the comment in line 1 of my post). At least this way i will learn better way to write it! Many features and shortcuts i don't know about, and my logic was kind of lame too. I misconstrued the method of subraction, i thought it was to subract every other line and not every line (ie: every two lines do a subtraction). so i had that interpreted wrongly. Thanks for your contribution!

Report •

#8
March 15, 2010 at 12:44:24
Works a treat, Razor2.3 thanks for your time to look at this. nbrane thanks for your contribution, I think Razor2.3 has given us both a masterclass in vbscript.

Thanks again!!!!


Report •

#9
March 17, 2010 at 10:56:22
Or, in bat:

-------------------------
@echo off > newfile & setLocal enableDELAYedeXpansion

for /f "tokens=1-4 delims=," %%a in (mydata) do (
if not defined S (
set S=%%d
) else (
set /a D=%%d-!S! & >> newfile echo.%%a,%%b,%%c,!D! & set S=%%d
)
)


=====================================
Helping others achieve escape felicity

M2


Report •

Ask Question