Solved

Parse file and delete any record that isn't a number as the first character..

Posted on 2006-11-29
2
183 Views
Last Modified: 2010-04-23
I have the text file below that I need to parse out any records that don't start with a numeric character.

* PURPOSE OTHER THAN COMPANY       *   * DIAL 222-7772 - THT/DB           *
* BUSINESS.                        *   * DIAL 222-7777 - CCC              *
************************************   ************************************
................................................................................
T - TECH SUPPORT  C - CONTRACT PROGRAMMER  * - OUTSIDE IS


USER     PROGRAMMER                 LONG    AREA TELEPHONE LOCATION
 CD TECH    NAME                  DISTANCE  CODE  NUMBER
................................................................................
1069   AARON SCOTT              762-4267 (814)
2366   ABIG JOHN                284-569 (862)
2664   ABOUDI MARY              535-3033 (814)
3941   ALGEE SHEREE             241-6665 (514)-->CELL
0237   ALT JAMES                425-6947 (814)
1843   AMBY DON                 821-3955 (862) PGR/222-2016 C/303-7435

F DAHL PROBLEMS
AMERINET PROBLEMS
AMERINET/MORRETIN  1-660-653-8008   GARY MORRETIN
AMERITECH/MULLINS  1-862-892-4432 STEVE MULLINS8
1137    AMISTAPUR GOPI          462-8507 (814)
2850   AMOD RAJA                736-8541 (814) 814-236-8523

The new file would look like this:

1069   AARON SCOTT              762-4267 (814)
2366   ABIG JOHN                284-569 (862)
2664   ABOUDI MARY              535-3033 (814)
3941   ALGEE SHEREE             241-6665 (514)-->CELL
0237   ALT JAMES                425-6947 (814)
1843   AMBY DON                 821-3955 (862) PGR/222-2016 C/303-7435
1137    AMISTAPUR GOPI          462-8507 (814)
2850   AMOD RAJA                736-8541 (814) 814-236-8523


thanks
0
Comment
Question by:rkckjk
  • 2
2 Comments
 
LVL 16

Expert Comment

by:Hillwaaa
ID: 18042500
Hi rkckjk,

Try this:

        Dim path As String = "C:\temp\somefile.txt"
        Dim pathOut As String = "C:\temp\somefileNEW.txt"

        Dim fs As IO.StreamReader
        Dim fsOut As IO.StreamWriter
        Dim str As String

        fs = New IO.StreamReader(IO.File.OpenRead(path))
        fsOut = New IO.StreamWriter(IO.File.OpenWrite(pathOut))
        While Not (fs.EndOfStream)
            str = fs.ReadLine
            If (str.StartsWith("1") Or str.StartsWith("2") Or str.StartsWith("3") Or str.StartsWith("4") Or str.StartsWith("5") Or str.StartsWith("6") Or str.StartsWith("7") Or str.StartsWith("8") Or str.StartsWith("9") Or str.StartsWith("0")) Then
                fsOut.WriteLine(str)
            End If
        End While

        fs.Close()
        fsOut.Close()

Cheers,
Hillwaaa
0
 
LVL 16

Accepted Solution

by:
Hillwaaa earned 500 total points
ID: 18042508
rkckjk,

If you need the new file to have the same name as the original, then just delete the original and rename the new as follows (after you have closed fs and fsout):

        IO.File.Delete(path)
        IO.File.Copy(pathOut, path)
        IO.File.Delete(pathOut)
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction As chip makers focus on adding processor cores over increasing clock speed, developers need to utilize the features of modern CPUs.  One of the ways we can do this is by implementing parallel algorithms in our software.   One recent…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.

733 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question