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?

Attachments
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
PUBLT     167_89              348       Pkt                 -41274.383  5533.260    349       PB                  DAUD                                                                                                200002231 269.1220104.032063.1220 084800103.341062.5710 084800283.3740297.1550084900284.1330297.322008490008490089.1220 103.521027.0940 269.12200.0135  -10.0855103.5150269.1146
PUBLT     167_89              348       Pkt                 -41274.383  5533.260    349       PB                  DAUD                                                                                                200002232 269.1220104.205061.4620 085400103.521061.3220 085400283.5920298.5700085500284.3510299.112008550008550089.1220 104.115328.4225 269.12200.0128  -10.0850104.1141269.1154
PUBLT     167_89              484       Pkt                 -42319.149  5583.337    485       PB                  DAUD                                                                                                200003092 101.423098.0830 59.5010 08590097.3500 59.3520 090000277.3650300.3830090100278.1410300.5340090200090100281.423097.5337 30.3140 101.42300.0122  -4.2640 97.5320 101.4158
PUBLT     167_89              484       Pkt                 -42319.149  5583.337    485       PB                  DAUD                                                                                                200003091 101.423097.5045 61.3410 08520097.2025 61.1930 085300277.2155298.5330085400277.5545299.0820085500085400281.423097.3713 28.4702 101.42300.0128  -4.2647 97.3655 101.4158
PUBLT     167_89              89        PB                  -40536.157  7619.924    90        PB                  DAUD                                                                                                200002082 279.0230111.203558.3150 090700110.494558.1650 090700290.5150301.5810090800291.2600302.130009080009080099.0230 111.070331.5038 279.02300.0117  -15.1538111.0745279.0252
PUBLT     167_89              89        PB                  -40536.157  7619.924    90        PB                  DAUD                                                                                                200002081 279.0230110.515060.0110 090100110.220059.4620 090100290.2345300.2840090200290.5755300.433009020009020099.0230 110.385330.2110 279.02300.0122  -15.1543110.3933279.0251
PUBLT     167_89              159       Pkt                 -42699.057  5820.319    160       Pkt                 DAUD                                                                                                200002101 222.5730252.201066.5830 170700251.511067.0355 17070071.5800 292.444517080072.3320 292.354517080017080042.5730 252.104022.4931 222.57300.0157  -14.3106252.0949222.5624
PUBLT     167_89              159       Pkt                 -42699.057  5820.319    160       Pkt                 DAUD                                                                                                200002102 222.5730252.312067.4035 171200252.015067.4425 17120072.0835 291.464017130072.4405 291.401017130017130042.5730 252.212822.0027 222.57300.0202  -14.3102252.2029222.5616
PUBLT     167_89              416       Pkt                 -43081.120  5025.695    417       Pkt                 DAUD                                                                                                200003012 194.2150102.240056.4210 091500101.482056.3450 091500281.5140303.3720091600282.3310303.441009160009160014.2150 102.091733.3107 194.21500.0112  -7.3153 102.0849194.2108
PUBLT     167_89              416       Pkt                 -43081.120  5025.695    417       Pkt                 DAUD                                                                                                200003011 194.2150102.085057.5530 091000101.322557.4810 091000281.3620302.2530091100282.1650302.351009110009110014.2150 101.533632.1915 194.21500.0116  -7.3157 101.5309194.2109
PUBLT     167_89              2         BKL       78/906    -40120.746  10203.556   1         BKL                 DAUD                                                                                                200002022 40.2700 114.104556.4300 091400113.350556.3400 091400293.3825303.3630091500294.1955303.4500091500091500220.2700113.560333.3107 40.2700 0.0112  -17.0324113.565740.2728 
PUBLT     167_89              2         BKL       78/906    -40120.746  10203.556   1         BKL                 DAUD                                                                                                200002021 40.2700 113.441557.5600 090900113.075557.4730 090900293.1145302.2430091000293.5215302.3300091000091000220.2700113.290332.1830 40.2700 0.0116  -17.0327113.296040.2731 
PUBLT     167_89              609       Pkt                 -39643.856  6947.341    610       Pkt                 DAUD                                                                                                200004242 165.202078.0935 68.1110 08440077.3605 67.5650 084500257.3715292.2010084600258.1355292.3510084700084600345.202077.5413 22.1150 165.20200.0201  12.5405 77.5533 165.2123
PUBLT     167_89              609       Pkt                 -39643.856  6947.341    610       Pkt                 DAUD                                                                                                200004241 165.202078.0940 69.3720 08390077.3630 69.2440 084000257.3740290.4940084100258.1350291.0420084200084100345.202077.5425 20.4300 165.20200.0211  12.5401 77.5547 165.2124
PUBLT     167_89              540       Pkt                 -39400.963  6473.620    541       Pkt                 DAUD                                                                                                200003272 304.254089.2730 66.1115 08390088.5235 65.5735 084000268.5210294.1645084100269.2705294.3140084200084100124.254089.0950 24.0954 304.25400.0149  2.3829  89.0929 304.2502
PUBLT     167_89              540       Pkt                 -39400.963  6473.620    541       Pkt                 DAUD                                                                                                200003271 304.254089.2100 67.3815 08330088.4625 67.2410 083400268.4605292.4950083500269.2040293.0435083600083500124.254089.0332 22.4300 304.25400.0157  2.3823  89.0311 304.2502
PUBLT     167_89              207       Pkt                 -43570.486  5306.496    208       Pkt                 DAUD                                                                                                200002142 230.5210253.493067.1020 171400253.214067.1605 17140073.3110 292.130017150074.0800 292.043517150017150050.5210 253.423522.2748 230.52100.0159  -13.1138253.4220230.5141
PUBLT     167_89              207       Pkt                 -43570.486  5306.496    208       Pkt                 DAUD                                                                                                200002141 230.5210253.391066.2555 171100253.112066.3315 17110073.2010 293.134017120073.5630 293.024017120017120050.5210 253.314823.1918 230.52100.0154  -13.1141253.3136230.5145
PUBLT     167_89              111       Pkt                 -40940.003  7012.614    112       Pkt                 DAUD                                                                                                200002082 225.4850251.082064.4425 170600250.383064.5015 17060070.4440 294.385017070071.1950 294.302017070017070045.4850 250.575024.5338 225.48500.0145  -15.0922250.5748225.4830
PUBLT     167_89              111       Pkt                 -40940.003  7012.614    112       Pkt                 DAUD                                                                                                200002081 225.4850251.040063.5230 170100250.323063.5950 17010070.2510 295.465517020070.5020 295.360017020017020045.4850 250.430025.5239 225.48500.0140  -15.0926250.4259225.4831
PUBLT     167_89              695       PB                  -38271.151  7051.168    696       PB                  DAUD                                                                                                200005161 82.3950 71.0450 62.3520 08490070.2840 62.3440 085000250.3030297.4020085100251.1010297.5510085200085100262.395070.4832 27.3622 82.3950 0.0133  19.0740 70.4802 82.3901 
PUBLT     167_89              695       PB                  -38271.151  7051.168    696       PB                  DAUD                                                                                                200005162 82.3950 70.5135 61.0510 08560070.1425 61.5010 085700250.1615299.2450085800251.1605299.3950085900085800262.395070.3935 29.0220 82.3950 0.0127  19.0744 70.3906 82.3902 
PUBLT     167_89              134       Pkt                 -41867.279  6475.067    135       Pkt                 DAUD                                                                                                200002092 204.5840251.410065.2655 171400251.112065.3320 17140071.1710 293.551517150071.5300 293.461017150017150024.5840 251.303824.1017 204.58400.0149  -14.5015251.2956204.5742
PUBLT     167_89              134       Pkt                 -41867.279  6475.067    135       Pkt                 DAUD                                                                                                200002091 204.5840251.234064.2420 170800250.545064.3215 17080071.0100 295.135517090071.3600 295.023017090017090024.5840 251.135225.1957 204.58400.0143  -14.5020251.1308204.5739
PUBLT     167_89              454       PB

open the file line input split the line with space delimeter put the data in the array then manipulate the array number.

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

Your file looks like a fixed-length file rather than space-delimited so I would not recommend using Split() but rather reading the positions. This is almost identical to your last thread except you're wanting to perform calculations. Copy the code from your last thread to read each segment of the line to local variables.

Are the two lines guaranteed to always be in order? If they are then you can "peek" ahead a single line to get the second set of values. If they are sometimes out of order you would probably want to digest the entire file in memory so you can look up other line numbers.

Give this a shot and post some code and we'll work from there. Also when you post test data you should use code tags to preserve formatting which is very important for text file parsing. If you're posting sample data use [code=text]:

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
PUBLT     167_89              348       Pkt                 -41274.383  5533.260    349       PB                  DAUD                                                                                                200002231 269.1220104.032063.1220 084800103.341062.5710 084800283.3740297.1550084900284.1330297.322008490008490089.1220 103.521027.0940 269.12200.0135  -10.0855103.5150269.1146
PUBLT     167_89              348       Pkt                 -41274.383  5533.260    349       PB                  DAUD                                                                                                200002232 269.1220104.205061.4620 085400103.521061.3220 085400283.5920298.5700085500284.3510299.112008550008550089.1220 104.115328.4225 269.12200.0128  -10.0850104.1141269.1154

Edited 3 Years Ago by happygeek: fixed formatting

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Const inFile As String = "C:\sob.txt"
        Const outFile As String = "C:\sob_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(5) As String
                        fields(0) = line.Substring(30, 5).Trim()
                        fields(1) = line.Substring(84, 5).Trim()
                        Dim chunk As String() = line.Substring(382, 8).Split(".")
                        fields(2) = chunk(0).Trim()
                        fields(3) = chunk(1).Substring(0, 2).Trim()
                        fields(4) = chunk(1).Substring(2, 2).Trim()
                        sw.WriteLine(String.Format("{0}-{1} {2}-{3}-{4}", fields))

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

    End Sub
End Class

This is my code... i dont have an idea to develop code to use the data from two lines to perform calculation...

So how to implement what you are saying here? im a little blur about your approach...

Edited 3 Years Ago by happygeek: fixed formatting

See if this helps:

Private Sub Button10_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button10.Click
    Const inFile As String = "C:\data\PUBLT167_89.txt"
    Using sr As New System.IO.StreamReader(inFile)
      Dim line1 As String
      Dim line2 As String
      Do
        line1 = sr.ReadLine()
        line2 = sr.ReadLine()
        If (line1 = String.Empty) Or (line2 = String.Empty) Then
          MessageBox.Show("Error. Not expecting empty line")
          Exit Sub
        ElseIf (line1.Length <> 390) Or (line2.Length <> 390) Then
          MessageBox.Show("Invalid line format")
          Exit Sub
        End If

        Dim l1 As LineSetup = LineSetup.ParseLine(line1)
        Dim l2 As LineSetup = LineSetup.ParseLine(line2)

        If (l1 Is Nothing) Or (l2 Is Nothing) Then
          MessageBox.Show("Parsing failed.")
          Exit Sub
        ElseIf (l1.LineNumber <> l2.LineNumber) Then
          MessageBox.Show("Did not find the same line numbers twice in a row")
        End If


        'Do your math here
        Dim iSomeVal = l1.Reading1 + l2.Reading2
        Console.WriteLine(l1.LineNumber)


        'Dim len As Integer = line.Length
        'Dim lineNumber1 As String = line.Substring(30, 3) + "-" + line.Substring(84, 3)
        'System.Diagnostics.Debugger.Break()
      Loop Until line1 Is Nothing Or line2 Is Nothing
    End Using
  End Sub

  Private Class LineSetup
    Private _linePart1 As Integer
    Private _linePart2 As Integer
    Private _reading1 As Integer
    Private _reading2 As Integer
    Private _reading3 As Integer
    Public ReadOnly Property LineNumber() As String
      Get
        Return String.Format("{0:F0}-{1:F0}", _linePart1, _linePart2)
      End Get
    End Property
    Public ReadOnly Property Reading1() As Integer
      Get
        Return _reading1
      End Get
    End Property
    Public ReadOnly Property Reading2() As Integer
      Get
        Return _reading2
      End Get
    End Property
    Public ReadOnly Property Reading3() As Integer
      Get
        Return _reading3
      End Get
    End Property
    Private Sub New()

    End Sub
    Public Shared Function ParseLine(ByVal line As String) As LineSetup
      If (String.IsNullOrEmpty(line)) Then
        Return Nothing
      ElseIf (line.Length <> 390) Then
        Return Nothing
      End If
      Dim result As New LineSetup()
      result._linePart1 = Convert.ToInt32(line.Substring(30, 3))
      result._linePart2 = Convert.ToInt32(line.Substring(84, 3))

      Dim chunk As String() = line.Substring(382, 8).Split(".")
      result._reading1 = Convert.ToInt32(chunk(0).Trim())
      result._reading2 = Convert.ToInt32(chunk(1).Substring(0, 2).Trim())
      result._reading3 = Convert.ToInt32(chunk(1).Substring(2, 2).Trim())

      Return result
    End Function
  End Class

here are my code with my calculation...

Imports System.Math


Public Class Form1
    Dim x0 As Double
    Dim x As Double
    Dim x1 As Integer
    Dim x2 As Double
    Dim x3 As Double
    Dim xx As Integer
    Dim xxx As Integer
    Dim xxs As Double
    Dim deg As Integer
    Dim deg1 As Double
    Dim deg2 As Double
    Dim rad1 As Double
    Dim rad2 As Double
    Dim avg As Double
    Dim degl As Double
    Dim minn As Integer
    Dim secc As Double
    Dim seccc As Integer




    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Const inFile As String = "C:\sob.txt"
        Using sr As New System.IO.StreamReader(inFile)
            'Using sw As New System.IO.StreamWriter("c:\sob_out.txt", False)
            Dim line1 As String
            Dim line2 As String
            Do
                line1 = sr.ReadLine()
                'MsgBox(line1)
                line2 = sr.ReadLine()
                'MsgBox(line2)
                If (line1 = String.Empty) Or (line2 = String.Empty) Then
                    MessageBox.Show("Error. Not expecting empty line")
                    Exit Sub
                ElseIf (line1.Length <> 390) Or (line2.Length <> 390) Then
                    MessageBox.Show("Invalid line format")
                    Exit Sub
                End If

                Dim l1 As LineSetup = LineSetup.ParseLine(line1)
                Dim l2 As LineSetup = LineSetup.ParseLine(line2)
                'MsgBox(l1)
                'MsgBox(l2)
                If (l1 Is Nothing) Or (l2 Is Nothing) Then
                    MessageBox.Show("Parsing failed.")
                    Exit Sub
                ElseIf (l1.LineNumber <> l2.LineNumber) Then
                    MessageBox.Show("Did not find the same line numbers twice in a row")
                End If


                'Do your math here
                'Dim iSomeVal = l1.Reading3 + l2.Reading3
                'MsgBox(iSomeVal)
                deg1 = l1.Reading1 + (l1.Reading2 / 60) + (l1.Reading3 / 3600)
                deg2 = l2.Reading1 + (l2.Reading2 / 60) + (l2.Reading3 / 3600)

                rad1 = deg1 * PI / 180
                rad2 = deg2 * PI / 180

                avg = (rad1 + rad2) / 2

                'degl = avg * 180 / PI
                'MsgBox(degl)
                Call dms(avg, deg, minn, secc)
                seccc = Round(secc, 1)
                'MsgBox(deg)
                'MsgBox(minn)
                'MsgBox(seccc)
                'Dim len As Integer = line.Length
                'Dim lineNumber1 As String = line.Substring(30, 3) + "-" + line.Substring(84, 3)
                'System.Diagnostics.Debugger.Break()
            Loop Until line1 Is Nothing Or line2 Is Nothing
        End Using
    End Sub

    Public Sub dms(ByVal xxxx As Double, ByRef deg As Integer, ByRef min As Integer, ByRef sec As Double)
        x = xxxx * 180 / PI
        'MsgBox(x)
        Call rounddown(x, xx)
        Call degree(x, xx, xxs)
        deg = xx
        'MsgBox(deg)
        Call rounddown(xxs, xxx)
        Call minute(xxx, x, deg, min, sec)
        'MsgBox(min)
        'MsgBox(sec)
    End Sub
    Public Sub rounddown(ByVal deci As Double, ByRef x4 As Integer)
        x0 = deci
        'MsgBox(deci)
        x1 = x0
        'MsgBox(x1)
        x2 = x0 - x1
        'MsgBox(x2)
        If x2 < 0 Then
            x3 = x0 - 1
            x4 = Round(x3)
            'MsgBox(x4)
        ElseIf x2 > 0 Then
            x4 = Round(x0)
        End If
    End Sub
    Public Sub degree(ByVal x As Double, ByVal xx As Integer, ByRef xxs As Double)
        deg = xx
        xxs = (x - deg) * 60
        'MsgBox(deg)
        'MsgBox(xxs)
    End Sub
    Public Sub minute(ByVal xxx As Integer, ByVal x As Double, ByVal deg As Integer, ByRef min As Integer, ByRef sec As Double)
        min = xxx
        sec = (((x - deg) * 60) - min) * 60
    End Sub

    Private Class LineSetup
        Private _linePart1 As Integer
        Private _linePart2 As Integer
        Private _reading1 As Integer
        Private _reading2 As Integer
        Private _reading3 As Integer
        Public ReadOnly Property LineNumber() As String
            Get
                Return String.Format("{0:F0}-{1:F0}", _linePart1, _linePart2)
            End Get
        End Property
        Public ReadOnly Property Reading1() As Integer
            Get
                Return _reading1
            End Get
        End Property
        Public ReadOnly Property Reading2() As Integer
            Get
                Return _reading2
            End Get
        End Property
        Public ReadOnly Property Reading3() As Integer
            Get
                Return _reading3
            End Get
        End Property
        Private Sub New()

        End Sub
        Public Shared Function ParseLine(ByVal line As String) As LineSetup
            If (String.IsNullOrEmpty(line)) Then
                Return Nothing
            ElseIf (line.Length <> 390) Then
                Return Nothing
            End If
            Dim result As New LineSetup()
            result._linePart1 = Convert.ToInt32(line.Substring(30, 3))
            result._linePart2 = Convert.ToInt32(line.Substring(84, 3))

            Dim chunk As String() = line.Substring(382, 8).Split(".")
            result._reading1 = Convert.ToInt32(chunk(0).Trim())
            result._reading2 = Convert.ToInt32(chunk(1).Substring(0, 2).Trim())
            result._reading3 = Convert.ToInt32(chunk(1).Substring(2, 2).Trim())

            Return result
        End Function
    End Class
End Class

but how can i export the data for Deg, Minn and Seccc to the output file?

i want to export with the format Deg-Minn-Seccc
For the example:
102-12-45
105-43-12

This question has already been answered. Start a new discussion instead.