Klausn
Klausn

Reputation: 3

Vbscript to replace a special value with the previous value in a csv file with multiple columns

i have a csv file like this:

Date;Time:From;To;Column1;Column2;Column3;Column4;
27.07.2015;07:00+01;07:15+01;10;3.23506;0;3.23506;
27.07.2015;07:15+01;07:30+01;15;3.22012;0;3.22012;
27.07.2015;07:30+01;07:45+01;10;3.4916;0;3.4916;
27.07.2015;07:45+01;08:00+01;34;3.86548;0;3.86548;
27.07.2015;08:00+01;08:15+01;undef;5.1987;0;5.1987;
27.07.2015;08:15+01;08:30+01;undef;6.66222;0;6.66222;
27.07.2015;08:30+01;08:45+01;undef;6.88191;0;6.88191;
27.07.2015;08:45+01;09:00+01;undef;6.91766;0;6.91766;
27.07.2015;09:00+01;09:15+01;17;3.86548;0;3.86548;
27.07.2015;09:15+01;09:30+01;undef;3.86548;0;3.86548;
27.07.2015;09:30+01;09:45+01;25;3.86548;0;3.86548;

How is it possible to replace the "undef" value with the last real value?

The Result should look like this:

Date;Time:From;To;Column1;Column2;Column3;Column4;
27.07.2015;07:00+01;07:15+01;10;3.23506;0;3.23506;
27.07.2015;07:15+01;07:30+01;15;3.22012;0;3.22012;
27.07.2015;07:30+01;07:45+01;10;3.4916;0;3.4916;
27.07.2015;07:45+01;08:00+01;34;3.86548;0;3.86548;
27.07.2015;08:00+01;08:15+01;34;5.1987;0;5.1987;
27.07.2015;08:15+01;08:30+01;34;6.66222;0;6.66222;
27.07.2015;08:30+01;08:45+01;34;6.88191;0;6.88191;
27.07.2015;08:45+01;09:00+01;34;6.91766;0;6.91766;
27.07.2015;09:00+01;09:15+01;17;3.86548;0;3.86548;
27.07.2015;09:15+01;09:30+01;17;3.86548;0;3.86548;
27.07.2015;09:30+01;09:45+01;25;3.86548;0;3.86548;

The script should also work if the undef is on an another position or the csv file has more columns.

Did anyone has an idea?

BR Klausn

Upvotes: 0

Views: 407

Answers (1)

Ekkehard.Horner
Ekkehard.Horner

Reputation: 38755

Use the first - hopefully complete - line to fill the gaps in the next line and use that for the next step:

Option Explicit

Dim goFS  : Set goFS = CreateObject("Scripting.FileSystemObject")
Dim tsIn  : Set tsIn = goFS.OpenTextFile("..\data\31557923.csv")
tsIn.SkipLine ' skip header (and two EOF checks and a check for undef in first line)
Dim aData : aData = Split(tsIn.ReadLine(), ";")
WScript.Echo Join(aData, ";")
Do Until tsIn.AtEndOfStream
   Dim aLine : aLine = Split(tsIn.ReadLine(), ";")
   Dim i
   For i = 0 To UBound(aData)
       If "undef" = aLine(i) Then aLine(i) = aData(i)
   Next
   aData = aLine
   WScript.Echo Join(aData, ";")
Loop
tsIn.Close

output:

cscript 31557923.vbs
27.07.2015;07:00+01;07:15+01;10;3.23506;0;3.23506;
27.07.2015;07:15+01;07:30+01;15;3.22012;0;3.22012;
27.07.2015;07:30+01;07:45+01;10;3.4916;0;3.4916;
27.07.2015;07:45+01;08:00+01;34;3.86548;0;3.86548;
27.07.2015;08:00+01;08:15+01;34;5.1987;0;5.1987;
27.07.2015;08:15+01;08:30+01;34;6.66222;0;6.66222;
27.07.2015;08:30+01;08:45+01;34;6.88191;0;6.88191;
27.07.2015;08:45+01;09:00+01;34;6.91766;0;6.91766;
27.07.2015;09:00+01;09:15+01;17;3.86548;0;3.86548;
27.07.2015;09:15+01;09:30+01;17;3.86548;0;3.86548;
27.07.2015;09:30+01;09:45+01;25;3.86548;0;3.86548;

Update:

A version that handles the header correctly and uses known default data for the first line:

Option Explicit

Sub checkEOF(tsIn)
  If tsIn.AtEndOfStream Then
     WScript.Echo "Incomplete file"
     WScript.Quit 1
  End If
End Sub


Dim goFS  : Set goFS = CreateObject("Scripting.FileSystemObject")
Dim tsIn  : Set tsIn = goFS.OpenTextFile("..\data\31557923-2.csv")
checkEOF tsIn
WScript.Echo tsIn.ReadLine()
checkEOF tsIn
Dim aData : aData = Split("27.07.2015;07:15+01;07:30+01;15;3.22012;0;3.22012;", ";")
Do Until tsIn.AtEndOfStream
   Dim aLine : aLine = Split(tsIn.ReadLine(), ";")
   Dim i
   For i = 0 To UBound(aData)
       If "undef" = aLine(i) Then aLine(i) = aData(i)
   Next
   aData = aLine
   WScript.Echo Join(aData, ";")
Loop
tsIn.Close

Upvotes: 2

Related Questions