i have a ASCII file that contain 3 line of numbers as below:
307.1642127.1642334.3412154.3412334.341288.2652 271.330896.104
127.1642307.1642285.1534105.1530285.153287.3500 272.2500214.227
285.1532105.1532307.1642127.1642307.164287.2001 272.395952.055


i just want to extract red number which is its stand for angle/azimuth ... so how can i do that in vb.net? the problem here is its not separated by any space or comma delimiter... so how could i extract this information?

Recommended Answers

All 27 Replies

How is that data structured? Are those numbers always guaranteed to be in the exact same position? If so you could read the middle of the string. Or will the number always start N positions after the N'th decimal number?

How is that data structured? Are those numbers always guaranteed to be in the exact same position? If so you could read the middle of the string. Or will the number always start N positions after the N'th decimal number?

yes... its always in same position.... actually the data structured are as below:
29 Pkt 28 Pkt 27 Pkt 285.1532105.1532307.1642127.1642307.164287.2001 272.395952.055 52.055
28 Pkt 29 Pkt 30 Pkt 105.1532285.1532264.450684.4457 264.450280.2100 279.390043.566 43.566

I want to extract those red data and then export it into a new output file with a new format as below:
29-28 307-16-42 52.055
28-29 264-45-02 43.566

Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
    Dim lines As String() = New String() { _
    "29 Pkt 28 Pkt 27 Pkt 285.1532105.1532307.1642127.1642307.164287.2001 272.395952.055 52.055", _
    "28 Pkt 29 Pkt 30 Pkt 105.1532285.1532264.450684.4457 264.450280.2100 279.390043.566 43.566"}

    For Each line As String In lines
      Dim fields(5) As String
      fields(0) = line.Substring(0, 2)
      fields(1) = line.Substring(7, 2)
      fields(2) = line.Substring(53, 3)
      fields(3) = line.Substring(57, 2)
      fields(4) = line.Substring(59, 2)
      fields(5) = line.Substring(84, 6)
      Console.WriteLine("{0}-{1} {2}-{3}-{4} {5}", fields)
    Next
  End Sub

Results in:

29-28 307-16-42 52.055
28-29 264-45-02 43.566

thanks for your fast response sknake... im really appreciated for ur helping... for your information that data are contain in the ASCII file.. so how can i read those strings from the file.. and one more thing is there are another data that i need to extract from the strings... that is the line code.. the example are as below:
29 Pkt 28 Pkt 27 Pkt 285.1532105.1532307.1642127.1642307.164287.2001 272.395952.055 52.055 2009120222
28 Pkt 29 Pkt 30 Pkt 105.1532285.1532264.450684.4457 264.450280.2100 279.390043.566 43.566 2009120214
so i want to add some data filtering in my software... if line code =22 then software will extract data from that line and export into new formatted ascii file.. then if the line code =14, that line will be ignored from the extraction process.. it means that the line with the code 14 will be not use...

Every time you post sample data it changes :P Please post _complete_ data.
Here is an example of filtering on line code 22, reading in from a file, and writing out to a file:

Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
    Const inFile As String = "C:\sampledata.txt"
    Const outFile As String = "C:\sampledata_new.txt"
    Using sr As New System.IO.StreamReader(inFile)
      Using sw As New System.IO.StreamWriter(outFile, False)
        Dim line As String
        Do
          line = sr.ReadLine()
          If Not String.IsNullOrEmpty(line) Then
            Dim fields(6) As String
            fields(0) = line.Substring(0, 2)
            fields(1) = line.Substring(7, 2)
            fields(2) = line.Substring(53, 3)
            fields(3) = line.Substring(57, 2)
            fields(4) = line.Substring(59, 2)
            fields(5) = line.Substring(84, 6)
            fields(6) = line.Substring(99, 2)
            If (Convert.ToInt32(fields(6)) = 22) Then 'If line # = 22
              sw.WriteLine(String.Format("{0}-{1} {2}-{3}-{4} {5} {6}", fields))
              'Console.WriteLine(String.Format("{0}-{1} {2}-{3}-{4} {5} {6}", fields))
            End If
          End If
        Loop Until line Is Nothing
        sw.Flush()
        sw.Close()
      End Using
    End Using
  End Sub

sorry my friend... here is i upload the real input file... so from the file you can see the structure of the data.. i have try your code and it come out with a little problem.. the problem is with the last line..
102 Pkt 129 Pkt 313.1320133.13201.0810 181.08101.0810 88.1805 271.415557.599 57.599 57.574 2000020211501325

the output file cannot be produce as below:

102-129 1-08-10 57.574

for the line no 6 its succesfully produced with the correct format.

That file is quite a bit different than what you posted here. All of your examples were using "Pkt" and there are "Bkl" lines in the file. Should those be ignored or treated the same? Can you also include examples for a few of the lines from data included in x.txt?

That file is quite a bit different than what you posted here. All of your examples were using "Pkt" and there are "Bkl" lines in the file. Should those be ignored or treated the same? Can you also include examples for a few of the lines from data included in x.txt?

ya... just ignore the other data... just extract data as before....

Here is my sample code..

Private Sub kira_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles kira.Click
        Const inFile As String = "C:\test2.fbk"
        Const outFile As String = "C:\x_new.DAT"
        Using sr As New System.IO.StreamReader(inFile)
            Using sw As New System.IO.StreamWriter(outFile, False)
                Dim line As String
                Do
                    line = sr.ReadLine()
                    If Not String.IsNullOrEmpty(line) Then
                        Dim fields(6) As String
                        fields(0) = line.Substring(60, 4)
                        fields(1) = line.Substring(90, 4)
                        fields(2) = line.Substring(152, 3)
                        fields(3) = line.Substring(155, 2)
                        fields(4) = line.Substring(157, 2)
                        fields(5) = line.Substring(196, 7)
                        fields(6) = line.Substring(220, 2)
                        If (Convert.ToInt32(fields(6)) = 12 Or (fields(6)) = 11 Or (fields(6)) = 15 Or (fields(6)) = 13 Or (fields(6)) = 23 Or (fields(6)) = 25) Then 'If line # = 12
                            sw.WriteLine(String.Format("BM {0}-{1} {2}-{3}-{4} {5} {6}", fields))
                            'Console.WriteLine(String.Format("{0}-{1} {2}-{3}-{4} {5} {6}", fields))
                        End If
                    End If
                Loop Until line Is Nothing
                sw.Flush()
                sw.Close()
            End Using
        End Using
    End Sub

And the result i get like this :
BM 2 -1 40.-27-30 80.961 11
BM 2 -1 40.-27-30 80.961 15
BM 2 -3 132-.0-90 49.769 12
BM 2 -4 218-.0-75 103.997 12
BM 2 -5 282-.5-65 66.004 12
BM 102 -129 1.0-81-0 57.574 25

so what i want actually are as below:
BM 2-1 40-27-30 80.961 11
BM 2-1 40-27-30 80.961 15
BM 2-3 132-09-00 49.769 12
BM 2-4 218-07-50 103.997 12
BM 2-5 282-56-50 66.004 12
BM 102-129 1-08-10 57.574 25

Ew, I see whats happening here. The decimal can float and they pad right with white spaces. Here is updated code.

'Imports System.Linq
  Private Sub Button5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button5.Click
    Const inFile As String = "C:\x.txt"
    Const outFile As String = "C:\x_new.txt"
    Dim lineCodes As Integer() = New Integer() {12, 11, 15, 13, 23, 25}
    Using sr As New System.IO.StreamReader(inFile)
      Using sw As New System.IO.StreamWriter(outFile, False)
        Dim line As String
        Do
          line = sr.ReadLine()
          If Not String.IsNullOrEmpty(line) Then
            Dim fields(6) As String
            fields(0) = line.Substring(60, 4).Trim()
            fields(1) = line.Substring(90, 4).Trim()

            '2-4 are a floating section in the file
            Dim chunk As String() = line.Substring(152, 8).Split(".")
            'fields(2) = line.Substring(152, 3).Trim() 'Problem
            'fields(3) = line.Substring(155, 2).Trim() 'Problem
            'fields(4) = line.Substring(157, 2).Trim() 'Problem
            fields(2) = chunk(0).Trim()
            fields(3) = chunk(1).Substring(0, 2).Trim()
            fields(4) = chunk(1).Substring(2, 2).Trim()
            fields(5) = line.Substring(196, 7).Trim()
            fields(6) = line.Substring(220, 2).Trim()


            Dim code As Integer = Convert.ToInt32(fields(6))
            If lineCodes.Contains(code) Then
              sw.WriteLine(String.Format("BM {0}-{1} {2}-{3}-{4} {5} {6}", fields))
              'Console.WriteLine(String.Format("[" + lineNbr.ToString() + "] " + "BM {0}-{1} {2}-{3}-{4} {5} {6}", fields))
            End If
          End If
        Loop Until line Is Nothing
        sw.Flush()
        sw.Close()
      End Using
    End Using
  End Sub

Be sure to add the Imports System.Linq to the top of the class.

Ew, I see whats happening here. The decimal can float and they pad right with white spaces. Here is updated code.

'Imports System.Linq
  Private Sub Button5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button5.Click
    Const inFile As String = "C:\x.txt"
    Const outFile As String = "C:\x_new.txt"
    Dim lineCodes As Integer() = New Integer() {12, 11, 15, 13, 23, 25}
    Using sr As New System.IO.StreamReader(inFile)
      Using sw As New System.IO.StreamWriter(outFile, False)
        Dim line As String
        Do
          line = sr.ReadLine()
          If Not String.IsNullOrEmpty(line) Then
            Dim fields(6) As String
            fields(0) = line.Substring(60, 4).Trim()
            fields(1) = line.Substring(90, 4).Trim()

            '2-4 are a floating section in the file
            Dim chunk As String() = line.Substring(152, 8).Split(".")
            'fields(2) = line.Substring(152, 3).Trim() 'Problem
            'fields(3) = line.Substring(155, 2).Trim() 'Problem
            'fields(4) = line.Substring(157, 2).Trim() 'Problem
            fields(2) = chunk(0).Trim()
            fields(3) = chunk(1).Substring(0, 2).Trim()
            fields(4) = chunk(1).Substring(2, 2).Trim()
            fields(5) = line.Substring(196, 7).Trim()
            fields(6) = line.Substring(220, 2).Trim()


            Dim code As Integer = Convert.ToInt32(fields(6))
            If lineCodes.Contains(code) Then
              sw.WriteLine(String.Format("BM {0}-{1} {2}-{3}-{4} {5} {6}", fields))
              'Console.WriteLine(String.Format("[" + lineNbr.ToString() + "] " + "BM {0}-{1} {2}-{3}-{4} {5} {6}", fields))
            End If
          End If
        Loop Until line Is Nothing
        sw.Flush()
        sw.Close()
      End Using
    End Using
  End Sub

Be sure to add the Imports System.Linq to the top of the class.

Its work... thanks a lot my friend....
if you have a time could you help me to solve my another thread..? because no one give the solution yet for that problem...

I can try, but no guarantees :P Message me the thread or post it here

I can try, but no guarantees :P Message me the thread or post it here

You can open the thread with title "Extract data from file and then append it to another file "
Actually that thread is extension of this thread. I want to extract data from another file and then append the extracted data into the output file from this thread.

I can try, but no guarantees :P Message me the thread or post it here

the thread are as below... im really hope that you will respond me whether you have a solution or not for this problem...

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
L,13207,11,02,06,000,6561.000
22,24,7,8,9,25,22
END
K,45,P
-18972.811,53723.643,BKL,,e-Coord,M
END

so how can i extract the red data and export it into a new output file with new structured as below:
C 1 -18970.811 53728.643
C 45 -18972.811 53723.643

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..

What I meant was post a link to the thread so I would know where it was, like:
http://www.daniweb.com/forums/thread249280.html

owh... im so sorry for misunderstanding... i have reply your request on that thread. and i have attached the input, output and old file for your reference. Thanks for helping. Im really appreciated that.

Its work... thanks a lot my friend....
if you have a time could you help me to solve my another thread..? because no one give the solution yet for that problem...

how can i put another condition in the loop to not extract the line if the fields(5) equals to 0.000 ?

Give it a shot and post the code you come up with and we'll go from there.

Sub Format_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles format.Click

        'Const inFile As String =""
        'Const outFile As String = "C:\x_newwww.txt"
        'WriteLine(String.Format("# Observations"))
        Dim lineCodes As Integer() = New Integer() {12, 11, 15, 13, 23, 25}
        Using sr As New System.IO.StreamReader(InputPath)
            Using sw As New System.IO.StreamWriter(datf, False)
                Dim line As String
                Do
                    line = sr.ReadLine()
                    If Not String.IsNullOrEmpty(line) Then
                        Dim fields(6) As String
                        fields(0) = line.Substring(60, 4).Trim()
                        fields(1) = line.Substring(90, 4).Trim()

                        '2-4 are a floating section in the file
                        Dim chunk As String() = line.Substring(152, 8).Split(".")
                        'fields(2) = line.Substring(152, 3).Trim() 'Problem
                        'fields(3) = line.Substring(155, 2).Trim() 'Problem
                        'fields(4) = line.Substring(157, 2).Trim() 'Problem
                        fields(2) = chunk(0).Trim()
                        fields(3) = chunk(1).Substring(0, 2).Trim()
                        fields(4) = chunk(1).Substring(2, 2).Trim()
                        fields(5) = line.Substring(196, 7).Trim()
                        fields(6) = line.Substring(220, 2).Trim()

                        'Dim distance As Double = Convert.ToInt32(fields(5))
                        'MsgBox(distance)
                        Dim code As Integer = Convert.ToInt32(fields(6))
                        If (lineCodes.Contains(code)) Then 'AndAlso (distance <> 0) Then
                            sw.WriteLine(String.Format("BM {0}-{1}  {2}-{3}-{4} {5}", fields))
                            'Console.WriteLine(String.Format("[" + lineNbr.ToString() + "] " + "BM {0}-{1} {2}-{3}-{4} {5} {6}", fields))
                        End If
                    End If
                Loop Until line Is Nothing
                sw.Flush()
                sw.Close()
            End Using
        End Using

that's my code... what i want to do is to put another condition besides to trace the line code.. i also want to filter the field(5).. so if the value of the field(5) equal to value 0.00 then maybe my system will ask the user either to continue the process by ignoring that line from extracting process or just stop the process to review back the input data... can i do that...

Try something like this:

Private Sub Button5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button5.Click
    Const inFile As String = "C:\x.txt"
    Const outFile As String = "C:\x_new.txt"
    Dim lineCodes As Integer() = New Integer() {12, 11, 15, 13, 23, 25}
    Using sr As New System.IO.StreamReader(inFile)
      Using sw As New System.IO.StreamWriter(outFile, False)
        Dim line As String
        Do
          line = sr.ReadLine()
          If Not String.IsNullOrEmpty(line) Then
            Dim fields(6) As String
            fields(0) = line.Substring(60, 4).Trim()
            fields(1) = line.Substring(90, 4).Trim()

            '2-4 are a floating section in the file
            Dim chunk As String() = line.Substring(152, 8).Split(".")
            'fields(2) = line.Substring(152, 3).Trim() 'Problem
            'fields(3) = line.Substring(155, 2).Trim() 'Problem
            'fields(4) = line.Substring(157, 2).Trim() 'Problem
            fields(2) = chunk(0).Trim()
            fields(3) = chunk(1).Substring(0, 2).Trim()
            fields(4) = chunk(1).Substring(2, 2).Trim()
            fields(5) = line.Substring(196, 7).Trim()
            fields(6) = line.Substring(220, 2).Trim()


            Dim code As Integer = Convert.ToInt32(fields(6))
            If lineCodes.Contains(code) Then
              System.Diagnostics.Debugger.Break()
              Dim distance As Decimal = Decimal.Parse(fields(5))
              If (distance = 0) Then
                If (MessageBox.Show("Distance equals 0. Do you want to import this record?", Me.Text, MessageBoxButtons.YesNo, MessageBoxIcon.Question) <> Windows.Forms.DialogResult.Yes) Then
                  Continue Do 'Skips writing the line
                End If
              End If
              sw.WriteLine(String.Format("BM {0}-{1} {2}-{3}-{4} {5} {6}", fields))
              'Console.WriteLine(String.Format("[" + lineNbr.ToString() + "] " + "BM {0}-{1} {2}-{3}-{4} {5} {6}", fields))
            End If
          End If
        Loop Until line Is Nothing
        sw.Flush()
        sw.Close()
      End Using
    End Using
  End Sub

i have attached the sample file for your reference... i have testing your code and after running the application, my VS2008 highlighted the code System.Diagnostics.Debugger.Break(). so whats the problem? can you test it with that sample file...

Oh just comment that line out, it tells the debugger to break. I was setting a breakpoint so I could modify the value in field(5) since the sample data I had didn't have a 0.0 value

ok.. its work... thanks a lot bro...
Actually i have another file that want to implement the extracting process like this thread. I have attached that file. The sample of data are as below:
PUBLT 167_89 656 PB -38321.478 6807.984 657 PB DAUD 200005041 278.271074.5150 69.4035 08370074.1830 69.2755 083800254.1925290.4620083900254.5615291.010008400008390098.2710 74.3630 20.3943 278.27100.0211 15.5954 74.3726 278.2748
PUBLT 167_89 656 PB -38321.478 6807.984 657 PB DAUD 200005042 278.271074.4940 68.1425 08430074.1610 68.0010 084400254.1720292.1650084500254.5400292.315508460008450098.2710 74.3418 22.0833 278.27100.0201 15.5958 74.3511 278.2746

what i gonna want to do is to extract those red data and then some simple calculation will be added because there are 2 lines for the same line number such as data for 656-657. The calculation are to get the average from the two values. The sample output are as below:

-this is data extracted for each line
656-657 278.2748
656-657 278.2746
-the final output i want is the average of those two line
656-657 278-27-47

For the calculation, the formula is as below:
278.2748 and 278.2746 are the value for bearing/ azimuth. It means 278 degree 27 minutes 48 seconds. So to get the average value of those two, we must consider the degree, minutes and seconds.
1. convert degree minutes seconds to decimal degrees
278+(27/60)+(48/3600) >>>> result is 278.4633333
278+(27/60)+(46/3600) >>>> result is 278.4627778

2. Convert to radian
278.4633333 x 180 / PI >>>>result is 4.860102013
278.4627778 x 180 / PI >>>>result is 4.860092316

3. get average
(4.860102013 + 4.860092316) / 2 >>>result is 4.860097165

4. Convert back average radian to degrees
4.860097165 x PI / 180 >>>>>>> result is 278.4630556

4. convert back degrees to degree minutes seconds
(for this formula i will create myself...)

the main problem here is i want to use 2 value from 2 line in one time to do that calculation... if those data in one line its not a problem.. how can i possibly do that?

Create another thread (the topic has changed) and post the code you have so far. Give this task a shot and we'll use the code you have as a starting point.

Create another thread (the topic has changed) and post the code you have so far. Give this task a shot and we'll use the code you have as a starting point.

sorry.. i dont have any code because i dont know where to start.. because i need to extract 2 lines at a time..
this is the new thread >>> http://www.daniweb.com/forums/post1092890.html#post1092890

1st of all try to extract the whole single line and then store it say line then try to extract the position of 4th decimal point(.) and store it a var, let say decpos then using mid function extract ur value i.e. mid(line, decpos+4,8).
Thus you got ur all eight digit.
You need to repeat the above process for each line & then you will get what you want.

May be i am wrong, i thought as per the sample lines given by u.
-Mukesh

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.