We help IT Professionals succeed at work.
Get Started

Cleaning up text file with Visual Basic .net

252 Views
Last Modified: 2013-11-26
I have a text file which contains the following:

<start>
-------------------------------------------------------
ID Num: BF00000000  Readings: 0000  Records: 000
Interval: 1 hour
------------------------------------------------------
Date & Time, Sample, Packet, ID, Reading, Status
------------------------------------------------------
ID Num: BF00000000  Readings: 0001  Records: 000
Interval: 1 hour
------------------------------------------------------
Date & Time, Sample, Packet, ID, Reading, Status
------------------------------------------------------
ID Num: CA07268139  Readings: 0004  Records: 001
Interval: 1 hour
------------------------------------------------------
Date & Time, Sample, Packet, ID, Reading, Status
2010-02-08 10:02, 0001, 001, 0007268139, 00000972, 063
2010-02-08 11:02, 0002, 001, 0007268139, 00000972, 063
2010-02-08 12:02, 0003, 001, 0007268139, 00000972, 063
2010-02-08 13:02, 0004, 001, 0007268139, 00000972, 063
2010-02-08 14:02, 0005, 001, 0007268139, 00000000, 000
2010-02-08 15:02, 0006, 001, 0007268139, 00000000, 000
2010-02-08 16:02, 0007, 001, 0007268139, 00000000, 000
2010-02-08 17:02, 0008, 001, 0007268139, 00000000, 000
<end>

Basically, any line that begins with a date needs to be kept.  All other information has to be discarded, so I will end up with a text file that looks like this:

2010-02-08 10:02, 0001, 001, 0007268139, 00000972, 063
2010-02-08 11:02, 0002, 001, 0007268139, 00000972, 063
2010-02-08 12:02, 0003, 001, 0007268139, 00000972, 063
2010-02-08 13:02, 0004, 001, 0007268139, 00000972, 063
2010-02-08 14:02, 0005, 001, 0007268139, 00000000, 000
2010-02-08 15:02, 0006, 001, 0007268139, 00000000, 000
2010-02-08 16:02, 0007, 001, 0007268139, 00000000, 000
2010-02-08 17:02, 0008, 001, 0007268139, 00000000, 000

What's the best way to do this?  I tried removing the junk lines with the attached code but it doesn't seem to work.  This is while reading the file line by line.  I've only included what I deem to be the relevant code (that does the cleaning).  I don't need to keep the header lines either.  Thanks a bunch!


If Microsoft.VisualBasic.Left(ioLine, 7) = "ID Num:" Then
                ioLine = ""
            End If
            If Microsoft.VisualBasic.Left(ioLine, 3) = "---" Then
                ioLine = ""
            End If
            If Microsoft.VisualBasic.Left(ioLine, 4) = "Date" Then
                ioLine = ""
            End If

Open in new window

Comment
Watch Question
Commented:
This problem has been solved!
Unlock 1 Answer and 11 Comments.
See Answer
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE