I have a file that contain data structure as below:
L,13206,11,02,06,000,3981.100
19,22,25,26,19
END
L,13207,11,02,06,000,6561.000
22,24,7,8,9,25,22
END
K,1,P
-18970.811,53728.643,BKL,,e-Coord,M
END

so how can i extract 1 and -18970.811,53728.643 and export it into a new output file with new structured as below:
C 1 -18970.811 53728.643
For your information, the value for -18970.811,53728.643 will be placed after string K,1,P or K,2,P or any number between K and P...

One more thing is i want to append that new formatted data into old ready file that already contain old data... new formatted data will be append to the top of the old file..

Recommended Answers

All 7 Replies

Can you upload a sample file with at least 5 records of data and what your desired output would be?

K,1,P
-18970.811,53728.643,BKL,,e-Coord,M
END
L,13207,11,02,06,000,6561.000
22,24,7,8,9,25,22
END
K,42,P
-18973.811,53722.643,BKL,,e-Coord,M
END

i want to extract the red data and export it as below:

C 1 -18970.811 53728.643
C 42 18973.811 53722.643

i have attached the input and output file for your reference. and im so sorry not provide you the thread link.

sorry... the output file should be as this new attached file... because the extracted data will be append to the old file... i forgot about that..

Writing to the beginning of file is not appending, that is inserting and is more expensive than writing to the end of file.

Imports System.IO

Public Class frmParser

  Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    Const inFile As String = "C:\data\dw\input.txt"
    Const outFile As String = "C:\data\dw\output_sk.txt"
    Const lineFmt As String = "C {0} {1} {2}"
    Dim tmpFilePath As String = Path.GetTempFileName()
    Dim bDataFound As Boolean = False

    Using sr As New StreamReader(inFile)
      Using sw As New StreamWriter(tmpFilePath, False)
        Dim line As String
        Do
          line = sr.ReadLine()
          If (line Is Nothing) Then Exit Do

          Dim fields As String() = line.Split(New [Char]() {","c}, System.StringSplitOptions.None)

          If (fields.Length = 3) AndAlso (String.Compare(fields(0), "K", True) = 0) AndAlso (String.Compare(fields(2), "P", True) = 0) Then
            Dim nextLine As String = sr.ReadLine()

            If (nextLine Is Nothing) Then
              MessageBox.Show("Bad input file. Expecting a new line after K,N,P", Me.Text, MessageBoxButtons.OK, MessageBoxIcon.Error)
              Exit Sub
            End If

            bDataFound = True
            Dim nextFields As String() = nextLine.Split(New [Char]() {","c}, System.StringSplitOptions.None)

            If (nextFields.Length <> 6) Then
              MessageBox.Show("Bad input file. Line following K,N,P was not 6 fields", Me.Text, MessageBoxButtons.OK, MessageBoxIcon.Error)
              Exit Sub
            End If

            sw.WriteLine(String.Format(lineFmt, fields(1), nextFields(0), nextFields(1)))


          End If
        Loop Until line Is Nothing
        sw.Close()
      End Using
      sr.Close()
    End Using

    'If we have an existing output file
    If (File.Exists(outFile)) Then
      'Rename the old file. Dont delete it in case something raises an exception so we have a backup
      File.Move(outFile, Path.ChangeExtension(outFile, ".old"))
      File.Move(tmpFilePath, outFile)
      'Append the existing input file to the new input file
      Using sw As New FileStream(outFile, FileMode.Append)
        Using sr As New FileStream(Path.ChangeExtension(outFile, ".old"), FileMode.Open)
          CopyStream(sr, sw)
        End Using
      End Using
      File.Delete(Path.ChangeExtension(outFile, ".old"))
    Else
      'No existing output file, just move it
      File.Move(tmpFilePath, outFile)
    End If

  End Sub

  Private Shared Sub CopyStream(ByRef inStream As Stream, ByRef outStream As Stream)
    Dim buffer(32768) As Byte
    Do
      Dim iBytesRead = inStream.Read(buffer, 0, buffer.Length)
      If (iBytesRead <= 0) Then Exit Do
      outStream.Write(buffer, 0, iBytesRead)
    Loop

  End Sub

End Class

Writing to the beginning of file is not appending, that is inserting and is more expensive than writing to the end of file.

Imports System.IO

Public Class frmParser

  Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    Const inFile As String = "C:\data\dw\input.txt"
    Const outFile As String = "C:\data\dw\output_sk.txt"
    Const lineFmt As String = "C {0} {1} {2}"
    Dim tmpFilePath As String = Path.GetTempFileName()
    Dim bDataFound As Boolean = False

    Using sr As New StreamReader(inFile)
      Using sw As New StreamWriter(tmpFilePath, False)
        Dim line As String
        Do
          line = sr.ReadLine()
          If (line Is Nothing) Then Exit Do

          Dim fields As String() = line.Split(New [Char]() {","c}, System.StringSplitOptions.None)

          If (fields.Length = 3) AndAlso (String.Compare(fields(0), "K", True) = 0) AndAlso (String.Compare(fields(2), "P", True) = 0) Then
            Dim nextLine As String = sr.ReadLine()

            If (nextLine Is Nothing) Then
              MessageBox.Show("Bad input file. Expecting a new line after K,N,P", Me.Text, MessageBoxButtons.OK, MessageBoxIcon.Error)
              Exit Sub
            End If

            bDataFound = True
            Dim nextFields As String() = nextLine.Split(New [Char]() {","c}, System.StringSplitOptions.None)

            If (nextFields.Length <> 6) Then
              MessageBox.Show("Bad input file. Line following K,N,P was not 6 fields", Me.Text, MessageBoxButtons.OK, MessageBoxIcon.Error)
              Exit Sub
            End If

            sw.WriteLine(String.Format(lineFmt, fields(1), nextFields(0), nextFields(1)))


          End If
        Loop Until line Is Nothing
        sw.Close()
      End Using
      sr.Close()
    End Using

    'If we have an existing output file
    If (File.Exists(outFile)) Then
      'Rename the old file. Dont delete it in case something raises an exception so we have a backup
      File.Move(outFile, Path.ChangeExtension(outFile, ".old"))
      File.Move(tmpFilePath, outFile)
      'Append the existing input file to the new input file
      Using sw As New FileStream(outFile, FileMode.Append)
        Using sr As New FileStream(Path.ChangeExtension(outFile, ".old"), FileMode.Open)
          CopyStream(sr, sw)
        End Using
      End Using
      File.Delete(Path.ChangeExtension(outFile, ".old"))
    Else
      'No existing output file, just move it
      File.Move(tmpFilePath, outFile)
    End If

  End Sub

  Private Shared Sub CopyStream(ByRef inStream As Stream, ByRef outStream As Stream)
    Dim buffer(32768) As Byte
    Do
      Dim iBytesRead = inStream.Read(buffer, 0, buffer.Length)
      If (iBytesRead <= 0) Then Exit Do
      outStream.Write(buffer, 0, iBytesRead)
    Loop

  End Sub

End Class

its awesome... you are so expert bro.... your code always suits my need very well... well done bro... and thanks a lot.... n happy new year too... :)
Do you mind to help me again in other problem after this? actually now im doing my thesis for my final year... fyi im not a IT or computer based student... that why im not expert in programming...

I'm glad the solution worked for you.

I don't mind helping on threads. It seems I have nothing better to do than post on Daniweb all on day ;) Just drop me the link to any new threads if you want me to take a look. I actually wouldn't have guessed you were a student or doing a project. Usually IT students need a project idea or ask for the code for some abstract implementation of a not-too-well-thought-out project. You had sample data and goals which led me to think it was for some type of in-house application. What was that data for anyway?

Please mark this thread as solved if you have found an answer to your question and good luck!

I'm glad the solution worked for you.

I don't mind helping on threads. It seems I have nothing better to do than post on Daniweb all on day ;) Just drop me the link to any new threads if you want me to take a look. I actually wouldn't have guessed you were a student or doing a project. Usually IT students need a project idea or ask for the code for some abstract implementation of a not-too-well-thought-out project. You had sample data and goals which led me to think it was for some type of in-house application. What was that data for anyway?

Please mark this thread as solved if you have found an answer to your question and good luck!

actually im try to develop a application for land survey purpose... that why the data contains many number... its stand for bearing and distance measurement.... thanks again... u really2 help me... ill let u know if i had another problem after this.. but actually u have already solved the most complicated part in my project... :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.