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

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.

Attachments
T,1
1,3,2,4,6,7,8,9,11,10,27,28,29,12,14,1
END
T,2
14,16,17,18,15,20,19,21,23,7
END
T,3
29,30,31,32,33,16
END
T,4
18,34,35,36,28
END
T,5
4,5
END
T,6
1,3,2,4,6,7,23,21,19,20,15,18,17,16,14,1
END
T,7
23,24
END
T,8
12,13
END
T,9
10,26
END
C,1
29,12
END
C,2
12,13
END
C,3
13,14
END
C,4
14,17
END
C,5
14,1
END
C,7
1,3
END
C,8
3,2
END
C,9
2,4
END
C,10
4,5
END
C,11
4,24
END
L,13200,11,02,06,000,995.300
17,18,34,32,33,17
END
L,13201,11,02,06,000,995.000
34,35,31,32,34
END
L,13202,11,02,06,000,995.000
35,36,30,31,35
END
L,13203,11,02,06,000,995.000
36,28,29,30,36
END
L,13204,11,02,06,000,3980.700
18,15,20,27,28,36,35,34,18
END
L,13205,11,02,06,000,3981.100
20,19,26,27,20
END
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,42,P
-18970.811,53728.643,BKL,,e-Coord,M
END

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

Attachments
BM 2-1  40-27-30 80.961
BM 2-1  40-27-30 80.961
BM 2-3  132-09-00 49.769
BM 2-4  218-07-50 103.997
BM 2-5  282-56-50 66.004
BM 3-6  127-14-40 58.923
BM 5-7  308-36-00 135.272
BM 5-2  282-56-50 66.004
BM 7-8  276-57-50 74.175
BM 7-5  308-36-00 135.272
BM 8-9  289-26-00 65.452
BM 8-7  276-57-50 74.175
BM 9-10  1-08-10 57.574
BM 9-8  289-26-00 65.452
BM 10-11  313-13-20 88.266
BM 10-9  1-08-10 57.574
BM 11-13  296-35-50 71.589
BM 11-10  313-13-20 88.266
BM 13-14  288-32-30 45.224
BM 13-11  296-35-50 71.589
BM 14-15  308-50-50 37.420
BM 14-13  288-32-30 45.224
BM 15-16  312-03-30 60.328
BM 15-14  308-50-50 37.420
BM 16-17  357-00-50 36.389
BM 16-15  312-03-30 60.328
BM 17-18  338-46-00 75.843
BM 17-16  357-00-50 36.389
BM 18-19  334-57-00 34.933
BM 18-17  338-46-00 75.843
BM 19-20  333-32-00 38.032
BM 19-18  334-57-00 34.933
BM 20-21  300-25-10 36.180
BM 20-19  333-32-00 38.032
BM 21-22  4-31-40 61.666
BM 21-20  300-25-10 36.180
BM 22-23  28-29-10 34.102
BM 22-21  4-31-40 61.666
BM 23-24  353-03-10 53.747
BM 23-22  28-29-10 34.102
BM 23-24  353-03-41 53.747
BM 24-25  1-50-42 48.976
BM 24-23  353-03-10 53.747
BM 25-26  11-02-42 63.801
BM 25-24  1-50-42 48.976
BM 26-27  334-34-12 96.069
BM 26-25  11-02-42 63.801
BM 27-28  307-16-42 51.999
BM 27-26  334-34-12 96.069
BM 28-29  285-15-32 214.036
BM 28-27  307-16-42 51.999
BM 29-30  264-45-02 42.950
BM 29-28  285-15-32 214.036
BM 30-31  266-30-02 75.965
BM 30-29  264-45-02 42.950
BM 31-32  249-34-42 164.473
BM 31-30  266-30-02 75.965
BM 32-33  243-42-12 157.321
BM 32-31  249-34-42 164.473
BM 33-34  268-49-02 95.176
BM 33-32  243-42-12 157.321
BM 34-35  286-26-32 94.365
BM 34-33  268-49-02 95.176
BM 35-36  295-23-32 57.336
BM 35-34  286-26-32 94.365
BM 36-37  228-33-32 31.823
BM 36-35  295-23-32 57.336
BM 37-38  276-27-52 127.010
BM 37-36  228-33-32 31.823
BM 38-40  270-07-42 65.422
BM 38-37  276-27-52 127.010
BM 40-41  280-27-12 60.960
BM 40-38  270-07-42 65.422
BM 40-41  280-27-17 60.960
BM 41-42  279-40-15 115.470
BM 41-40  280-27-12 60.960
BM 42-43  253-55-25 44.134
BM 42-41  279-40-15 115.470
BM 43-44  274-47-45 57.213
BM 43-42  253-55-25 44.130
BM 44-45  275-58-45 16.008
BM 44-43  274-47-45 57.210
BM 45-46  274-58-45 45.025
BM 45-44  275-58-45 16.010
BM 46-47  277-10-25 32.297
BM 46-45  274-58-45 45.030
BM 47-48  275-46-35 41.818
BM 47-46  277-10-25 32.300
BM 48-49  210-45-25 22.747
BM 48-47  275-46-35 41.820
BM 49-50  273-21-05 38.970
BM 49-48  210-45-25 22.750
BM 50-51  282-54-45 54.565
BM 50-49  273-21-05 38.970
BM 51-52  261-36-55 74.050
BM 51-50  282-54-45 54.560
BM 52-53  274-44-05 36.806
BM 52-51  261-36-55 74.050
BM 53-54  269-46-55 57.389
BM 53-52  274-44-05 36.810
BM 54-55  257-27-05 59.266
BM 54-53  269-46-55 57.390
BM 55-56  260-37-45 66.880
BM 55-54  257-27-05 59.270
BM 56-57  269-35-05 13.747
BM 56-55  260-37-45 66.880
BM 57-58  179-10-05 33.686
BM 57-56  269-35-05 13.750
BM 58-59  173-10-55 58.487
BM 59-60  166-37-35 41.912
BM 60-61  194-07-05 66.250
BM 61-62  193-23-05 33.406
BM 61-63  193-23-05 4.324
BM 61-62  193-23-05 33.406
BM 63-62  193-23-05 29.082
BM 62-64  177-33-15 33.404
BM 64-65  186-50-15 30.621
BM 64-65  186-49-59 30.621
BM 65-66  240-03-56 31.063
BM 66-67  156-08-46 39.540
BM 67-68  173-42-16 41.975
BM 68-69  66-43-16 16.693
BM 69-70  183-21-46 69.937
BM 70-71  170-45-36 44.369
BM 71-72  182-27-06 34.841
BM 72-73  177-20-46 52.078
BM 73-74  185-29-46 41.171
BM 74-75  178-26-56 31.083
BM 75-76  190-31-26 59.511
BM 76-77  191-31-16 40.365
BM 77-78  167-40-06 41.382
BM 78-79  163-32-16 32.829
BM 79-80  172-34-06 102.258
BM 80-81  208-54-06 53.968
BM 81-82  166-17-56 40.740
BM 82-83  173-39-26 29.643
BM 83-84  146-20-46 24.368
BM 84-85  180-50-46 34.168
BM 85-86  219-44-46 29.031
BM 86-87  177-45-16 87.463
BM 87-88  176-51-06 69.418
BM 88-89  180-26-06 29.269
BM 89-90  279-02-46 77.663
BM 89-90  279-02-52 77.663
BM 90-91  279-16-44 55.193
BM 91-92  290-18-24 41.617
BM 92-93  268-28-44 55.861
BM 93-94  276-52-24 58.648
BM 94-95  283-42-34 57.697
BM 95-96  271-02-14 28.676
BM 96-97  274-54-04 49.019
BM 97-98  281-36-04 32.582
BM 98-99  287-02-24 31.152
BM 99-100  273-08-04 22.273
BM 100-101  186-33-14 25.571
BM 101-102  181-41-34 79.217
BM 102-103  181-27-54 52.168
BM 103-104  188-21-24 31.528
BM 104-105  181-17-24 37.062
BM 105-106  186-48-14 47.602
BM 106-107  175-43-54 35.764
BM 107-108  184-47-54 38.547
BM 108-109  187-20-44 50.953
BM 109-110  224-25-54 39.524
BM 110-111  223-58-34 76.567
BM 111-112  225-48-44 78.291
BM 111-112  225-48-30 78.291
BM 112-113  171-28-14 43.920
BM 113-114  153-53-54 28.541
BM 114-115  207-19-44 44.838
BM 115-116  219-16-14 51.558
BM 116-117  222-03-04 48.811
BM 117-118  213-17-34 30.880
BM 118-119  195-23-24 28.046
BM 119-120  201-21-14 49.555
BM 120-121  190-53-04 58.961
BM 121-122  209-55-54 43.369
BM 122-123  200-11-14 78.106
BM 123-124  194-37-44 26.072
BM 124-125  194-58-14 72.187
BM 125-126  211-02-54 58.781
BM 126-127  214-26-14 52.253
BM 127-128  222-11-44 71.497
BM 128-129  212-35-44 70.321
BM 129-130  212-47-44 39.274
BM 130-131  234-26-54 40.682
BM 131-132  214-19-04 34.717
BM 132-133  262-11-14 41.753
BM 133-134  223-07-54 38.431
BM 134-135  204-58-54 31.037
BM 134-135  204-57-40 31.037
BM 135-136  185-40-14 49.956
BM 136-137  216-45-44 41.120
BM 137-138  216-31-14 60.059
BM 138-139  223-36-54 30.591
BM 139-140  219-04-54 31.817
BM 140-141  227-57-14 44.576
BM 141-142  239-50-34 38.326
BM 142-143  209-12-24 46.010
BM 143-144  221-36-24 31.458
BM 144-145  221-00-14 65.777
BM 145-146  226-51-24 70.832
BM 146-147  202-09-54 44.453
BM 147-148  166-19-44 30.301
BM 148-149  246-45-44 29.687
BM 149-150  227-22-34 68.085
BM 150-151  212-55-14 66.229
BM 151-152  217-33-34 45.634
BM 152-153  226-33-04 30.303
BM 153-154  214-26-04 28.126
BM 154-155  222-51-54 48.523
BM 155-156  272-29-44 63.687
BM 156-157  178-49-54 35.507
BM 157-158  181-24-24 35.180
BM 158-159  224-15-14 65.527
BM 159-160  222-58-44 63.366
BM 159-160  222-56-20 63.366
BM 160-161  222-59-24 61.456
BM 161-162  222-35-14 63.339
BM 162-163  236-38-24 60.921
BM 163-164  197-11-54 33.421
BM 164-165  222-37-04 71.485
BM 165-166  223-10-54 26.208
BM 166-167  217-23-44 31.720
BM 167-168  230-44-04 35.358
BM 168-169  224-41-14 31.199
BM 169-170  219-45-14 47.800
BM 170-171  122-20-24 75.432
BM 171-172  142-42-44 30.158
BM 172-173  123-17-14 59.993
BM 173-174  110-34-14 31.570
BM 174-175  142-50-24 34.304
BM 175-176  125-08-44 38.442
BM 176-177  126-33-04 49.915
BM 177-178  124-10-34 55.592
BM 178-179  131-34-34 41.570
BM 179-180  81-04-04 30.697
BM 180-181  114-23-04 32.389
BM 181-182  171-22-24 26.751
BM 182-183  118-24-14 31.458
BM 183-184  125-19-24 55.659
BM 183-184  125-15-57 55.659
BM 184-185  124-12-47 35.248
BM 185-186  125-57-27 73.255
BM 186-187  172-13-27 37.679
BM 187-188  66-35-07 30.675
BM 188-189  126-08-07 64.102
BM 189-190  133-15-17 28.988
BM 190-191  180-05-17 26.975
BM 191-192  270-32-17 49.843
BM 192-193  282-12-07 28.885
BM 193-194  280-26-27 96.511
BM 194-195  273-37-37 40.026
BM 195-196  235-37-37 46.687
BM 196-197  270-03-57 75.108
BM 193-198  196-29-57 6.784
BM 193-198  196-29-57 6.784
BM 197-199  272-10-17 65.903
BM 199-200  270-02-37 48.869
BM 200-201  265-37-47 30.761
BM 201-202  269-36-07 46.008
BM 202-203  269-13-27 109.015
BM 203-204  269-17-27 50.179
BM 204-205  265-51-37 62.218
BM 205-206  283-25-47 31.400
BM 206-207  281-22-07 53.783
BM 207-208  230-55-37 46.212
BM 207-208  230-51-43 46.212
BM 208-209  266-20-14 50.419
BM 209-210  256-43-04 59.589
BM 210-211  272-58-54 56.084
BM 211-212  310-18-54 50.632
BM 57-213  0-07-40 49.438
BM 213-214  359-59-30 74.803
BM 214-215  354-26-40 66.062
BM 215-216  4-46-40 70.820
BM 216-217  1-37-30 60.562
BM 217-218  2-39-40 60.560
BM 218-219  349-56-20 33.860
BM 219-220  0-57-40 81.089
BM 220-221  0-04-40 67.038
BM 221-222  0-04-20 57.744
BM 222-223  359-38-50 31.476
BM 223-224  0-39-00 59.226
BM 224-225  348-21-00 31.621
BM 225-226  1-43-50 114.900
BM 226-227  1-21-40 63.782
BM 227-228  1-06-10 85.435
BM 228-229  1-02-40 64.211
BM 229-230  359-52-00 39.799
BM 230-231  358-44-00 43.248
BM 231-232  357-41-30 38.214
BM 232-233  349-39-40 38.540
BM 233-234  338-35-10 68.948
BM 234-235  306-49-00 45.182
BM 235-236  256-13-10 29.081
BM 236-237  269-10-20 73.715
BM 236-237  269-09-31 73.715
BM 237-238  268-54-29 81.978
BM 238-239  270-25-09 69.559
BM 239-240  270-28-29 26.959
BM 240-241  270-26-59 31.955
BM 241-242  268-48-29 49.178
BM 242-243  270-48-49 64.196
BM 243-244  270-30-59 35.082
BM 244-245  269-23-09 85.518
BM 245-246  277-36-09 62.974
BM 246-247  267-51-09 41.281
BM 247-248  271-17-19 92.769
BM 248-249  265-15-09 31.864
BM 249-250  265-41-19 35.838
BM 250-251  274-18-49 52.329
BM 251-252  268-28-39 109.042
BM 252-253  267-45-39 74.918
BM 253-254  270-13-19 77.435
BM 254-255  270-02-49 81.126
BM 255-256  270-25-39 57.545
BM 256-257  270-33-59 122.895
BM 257-258  272-45-19 101.783
BM 257-258  272-44-08 101.783
BM 258-259  265-26-51 18.504
BM 259-260  265-37-51 56.048
BM 260-261  274-31-11 92.078
BM 261-262  266-09-51 41.602
BM 262-263  266-08-01 67.565
BM 263-264  269-56-51 120.089
BM 264-265  271-53-51 50.253
BM 265-266  180-01-01 96.801
BM 266-267  180-07-01 30.295
BM 267-268  180-30-11 46.548
BM 268-269  179-54-01 29.582
BM 269-270  175-21-21 71.020
BM 270-271  183-20-11 61.994
BM 271-272  183-17-11 37.929
BM 272-273  180-29-01 74.613
BM 273-274  178-21-11 83.614
BM 274-275  182-26-51 42.755
BM 275-276  180-01-11 68.450
BM 276-277  177-42-31 40.887
BM 277-278  181-27-21 55.366
BM 278-279  180-20-51 35.332
BM 279-280  180-31-51 101.538
BM 280-281  179-58-41 66.557
BM 281-282  180-19-01 29.478
BM 281-282  180-18-21 29.478
BM 282-283  180-50-10 90.361
BM 283-284  176-35-40 43.986
BM 284-285  186-07-10 48.680
BM 285-286  177-59-50 47.113
BM 286-287  176-04-40 50.145
BM 287-288  166-15-30 32.190
BM 288-289  182-08-40 58.481
BM 289-290  181-49-00 49.275
BM 290-291

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

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