简体   繁体   中英

How to find the Line number that starts with specific character from a text file in VB.NET?

There is a text file that having records more than 10,000 lines.

Each line starts with a specific number like 1,2,3,4 and 5. Each number indicates whether it's a header, main data, file detail, footer etc.

So, we need to find out the line number that starts with specific character, so that I can assign that value to read the text line for specific things.

SSIS Solution

Add a variable that increment by 1 on each line and check the first character using Substring function.

Assuming that the first column name is Column0

You can add a script component in the DataFlow Task and use a similar code: (using Vb.Net)

Dim intR As Integer = 0

Public Overrides Sub Input0_ProcessInputRow(ByVal Row As Input0Buffer)


    intR += 1

    If Not Row.Column0_IsNull AndAlso
           Not String.IsNullOrEmpty(Row.Column0.Trim) Then

        Select Case Row.Column0.Substring(0, 1)


            Case "1"

                'Line number is stored in intR

            Case "2"

                ...

           Case Else


        End Select

    End If




End Sub

Simple Vb app Solution

If working with an vb.net application, you can use StreamReader to read the textfile line by line

Using sr as new Io.StreamReader("C:\File1.txt")

    Dim intR as Integer = 0

    While Not sr.EndOfStream

        intR += 1

        dim strLine as string = sr.ReadLine()

        Select Case strLine.Substring(0,1)

            Case "1"
               'Line number is stored in intR

            Case "2"

            ...


        End Select

    End While


End Using

If there is a need for manipulation i would save all data inside a data structure (in my example HashSet ).
each Line as Line object ( Structure Line ) with 2 fields, OperationNumberNumber and LineText .
now that everything is arranged inside the hashset you can manipulate, sort, use Linq or Lambda expressions etc.. in an efficient manner.

Private fileData As New HashSet(Of Line)

Private Sub Foo()
    Dim TextLine As String = String.Empty
    Dim objReader As New System.IO.StreamReader(FilePath)

    Do While objReader.Peek() <> -1
        ' read line '
        TextLine = objReader.ReadLine()
        If Not (String.IsNullOrEmpty(TextLine)) Then
        ' create line object '
        Dim line = New Line()
        ' extract the line number '
        line.OperationNumberNumber = TextLine.Substring(0, 1)
        ' extract the line text '
        line.LineText = TextLine.Substring(1, TextLine.Length - 1)
        ' save to hashset '
        fileData.Add(line)
        End If
    Loop

End Sub

Structure Line
    Public OperationNumberNumber As Integer
    Public LineText As String
End Structure

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM