Results 1 to 29 of 29

Thread: How to read a formatted text file and group

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Dec 2014
    Posts
    82

    How to read a formatted text file and group

    What I have is a configuration text file that I created using some formatting.

    Example lines in text file are below - ; is used as a delimeter

    There are several prefixes that tell me what type of control the data is for
    [tbp - This is for a page on a tab control
    [grp - This is for a groupbox control
    [tbc - This is for a Tab Control
    [dgv - This is for a Datagridview control

    Right now I am only interested in the datagridview control. Lines in the text file that start with [dgv

    [dgvCh1Rec1];False;;1;0101;WID;Well Identifier;;;;;;
    [dgvCh1Rec1];False;;2;0102;SKNO;Sidetrack/Hole Section No.;;;;;;
    [dgvCh1Rec2];False;;1;0201;WID;Well Identifier;;;;;;
    [dgvCh1Rec2];False;;2;0202;SKNO;Sidetrack/Hole Section No.;;;;;;
    [dgvCh2Rec1];False;;1;0101;WID;Well Identifier;;;;;;
    [dgvCh2Rec1];False;;2;0102;SKNO;Sidetrack/Hole Section No.;;;;;;
    [dgvCh2Rec2];False;;1;0201;WID;Well Identifier;;;;;;
    [dgvCh2Rec2];False;;2;0202;SKNO;Sidetrack/Hole Section No.;;;;;;

    I am just showing Rec1 and Rec2 for Ch1 and Ch2 here but in reality it can be ChX and RecX Where X can be whatever

    So I am looking for a way read the text file and whenever [dgv is found group by Ch and Rec and put into an array.

    So 1st array would be Ch1Rec1 lines of text, 2nd array would be Ch1 Rec2 lines of text, 3rd array would be Ch2 Rec1 lines of text, 4th array would be Ch2 Rec2 lines of text.

    Thanks.

  2. #2
    Hyperactive Member
    Join Date
    Mar 2012
    Posts
    311

    Re: How to read a formatted text file and group

    Take a look at the TextFieldParser Class. This is a quick way of reading in delimited text files into an array for each line. You can then check the String in the first element of the array to determine whether it starts with "[dgv", and keep those arrays. See here for an example of using that class...

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Dec 2014
    Posts
    82

    Re: How to read a formatted text file and group

    That's nice and thanks but how do I get array for Ch1Rec1 lines of text. Next array for Ch1Rec2 lines of text. Next array for Ch1Rec3 lines of text and on and on.
    Then array for Ch2Rec1 lines of text, etc. Remember Ch goes from 1 to X and Rec goes from 1 to X.

  4. #4

    Thread Starter
    Lively Member
    Join Date
    Dec 2014
    Posts
    82

    Re: How to read a formatted text file and group

    Here is a easier example.

    Sample Text File

    1;Dog;Duke
    1;Cat;Kitty
    1;Hampster;Mousey
    2;Tiger;Mike
    2;Lion;Mufasa

    I want to read the entire textfile and create 2 arrays or datatables doesn't matter.

    The first array should contain rows where firstcolumn = 1
    The second array should contain rows where firstcolumn = 2

    Sample input here has only 5 lines in the text file but the text file can be any length.

    Also firstcolumn can go from 1 to any length.

  5. #5
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,479

    Re: How to read a formatted text file and group

    try this:

    Code:
    Imports Microsoft.VisualBasic.FileIO
    
    Public Class Form1
    
        Dim newLists As New List(Of List(Of String()))
    
        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            Dim tfp As New TextFieldParser("filename.txt")
            tfp.Delimiters = New String() {";"}
            Dim lines As New List(Of String())
            While Not tfp.EndOfData
                lines.Add(tfp.ReadFields)
            End While
            Dim unique() As String = lines.Select(Function(l) l(0)).Distinct.ToArray
    
            For Each s As String In unique
                newLists.Add(lines.Where(Function(l) l(0) = s).ToList)
            Next
            ComboBox1.Items.AddRange(unique)
        End Sub
    
        Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ComboBox1.SelectedIndexChanged
            Dim dt As New DataTable
            dt.Columns.Add("c1")
            dt.Columns.Add("c2", GetType(Boolean))
            For x As Integer = 3 To newLists(ComboBox1.SelectedIndex).Max(Function(c) c.Length)
                dt.Columns.Add("c" & x.ToString)
            Next
            For Each a As Object() In newLists(ComboBox1.SelectedIndex)
                dt.Rows.Add(a)
            Next
            DataGridView1.DataSource = dt
        End Sub
    
    End Class
    Last edited by .paul.; Feb 25th, 2015 at 09:21 PM.

  6. #6

    Thread Starter
    Lively Member
    Join Date
    Dec 2014
    Posts
    82

    Re: How to read a formatted text file and group

    Ok Paul I will tomorrow when I get to work.

    That last example only shows small amount of input from file as an example. It needs to handle
    1;Dog;Duke
    1;Cat;Kitty
    1;Hampster;Mousey
    2;Tiger;Mike
    2;Lion;Mufasa
    3;Bear;Smokey
    3;PolarBear;Snowflake
    4;Bird;Peetie
    4;Hawk;Screetch
    5;
    5;
    ...
    ...
    ...
    20;Hippo;Henreata

    and on and on till end of file

  7. #7
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,479

    Re: How to read a formatted text file and group

    there are no limitations in the number of distinct lists it creates, and there are no limitations on how many columns are used

  8. #8
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,479

    Re: How to read a formatted text file and group

    it is limited by the second column being a Boolean, but you should be able to change that quite easily.

  9. #9

    Thread Starter
    Lively Member
    Join Date
    Dec 2014
    Posts
    82

    Re: How to read a formatted text file and group

    So 1st array (or datatable) would have 3 elements - first column is 1, second array would have 2 elements - first column is 2, 20th array would have 1 element.

    Basically I want to read a file as input and group the lines of text into individual arrays based on the id used in the first column. The first column id could be numerical as show in my easy example or string as [dgvCh1Rec1] as I have in my other example input. So I would want to group on [dgvCh1Rec1], [dgvCh1Rec2], etc...

    Thanks for the input.

  10. #10

    Thread Starter
    Lively Member
    Join Date
    Dec 2014
    Posts
    82

    Re: How to read a formatted text file and group

    The first column is basically used as a separator for grouping purposes.

  11. #11

    Thread Starter
    Lively Member
    Join Date
    Dec 2014
    Posts
    82

    Re: How to read a formatted text file and group

    Paul thanks for the sample code. I am sure I can wiggle it around a bit and get it to suit my purpose. I was trying to implement a read forward to see if the next line to be read had a first column value that was different than the line that was just read and use that as a separator. Some days your on and some days your off. Today was an off day. Thanks again for all the input.

  12. #12
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,479

    Re: How to read a formatted text file and group

    I used the first set of data to test it but it should work with the animal text file too.
    It does separate into groups based on the first column. Try it and see...

  13. #13

    Thread Starter
    Lively Member
    Join Date
    Dec 2014
    Posts
    82

    Re: How to read a formatted text file and group

    ok. I will use the [dgvCh1Rec1] sample input for testing the code then. Again thanks for the assistance. It's 9:20pm here so I will try first thing when I get to work in the morning.

  14. #14

    Thread Starter
    Lively Member
    Join Date
    Dec 2014
    Posts
    82

    Re: How to read a formatted text file and group

    Paul the sample code worked great.

    Using this sample data again

    [dgvCh1Rec1];False;;1;0101;WID;Well Identifier;;;;;;
    [dgvCh1Rec1];False;;2;0102;SKNO;Sidetrack/Hole Section No.;;;;;;
    [dgvCh1Rec2];False;;1;0201;WID;Well Identifier;;;;;;
    [dgvCh1Rec2];False;;2;0202;SKNO;Sidetrack/Hole Section No.;;;;;;
    [dgvCh2Rec1];False;;1;0101;WID;Well Identifier;;;;;;
    [dgvCh2Rec1];False;;2;0102;SKNO;Sidetrack/Hole Section No.;;;;;;
    [dgvCh2Rec2];False;;1;0201;WID;Well Identifier;;;;;;
    [dgvCh2Rec2];False;;2;0202;SKNO;Sidetrack/Hole Section No.;;;;;;

    instead of separating on distinct first column which is basically separating on ChXRecX what if I wanted to separate on just ChX. I want to group by dgvCh1, dgvCh2, dgvCh3, etc.

  15. #15
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,479

    Re: How to read a formatted text file and group

    this should work:

    Code:
    Imports Microsoft.VisualBasic.FileIO
    
    Public Class Form1
    
        Dim newLists As New List(Of List(Of String()))
    
        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            Dim tfp As New TextFieldParser("C:\Users\Paul\Desktop\1.txt")
            tfp.Delimiters = New String() {";"}
            Dim lines As New List(Of String())
            While Not tfp.EndOfData
                lines.Add(tfp.ReadFields)
            End While
            Dim unique() As String = lines.Select(Function(l) l(0).Substring(1, l(0).IndexOf("Rec") - 1)).Distinct.ToArray
    
            For Each s As String In unique
                newLists.Add(lines.Where(Function(l) l(0).Substring(1, l(0).IndexOf("Rec") - 1) = s).ToList)
            Next
            ComboBox1.Items.AddRange(unique)
        End Sub
    
        Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ComboBox1.SelectedIndexChanged
            Dim dt As New DataTable
            dt.Columns.Add("c1")
            dt.Columns.Add("c2", GetType(Boolean))
            For x As Integer = 3 To newLists(ComboBox1.SelectedIndex).Max(Function(c) c.Length)
                dt.Columns.Add("c" & x.ToString)
            Next
            For Each a As Object() In newLists(ComboBox1.SelectedIndex)
                dt.Rows.Add(a)
            Next
            DataGridView1.DataSource = dt
        End Sub
    
    End Class

  16. #16

    Thread Starter
    Lively Member
    Join Date
    Dec 2014
    Posts
    82

    Re: How to read a formatted text file and group

    .Paul that code worked fine yesterday. I could trouble you one last time.

    Sample input from text file
    [grpConfigCh1];txtTCPIPPortCh1;
    [grpConfigCh1];txtIPAddressCh1;
    [grpConfigCh1];cboModeCh1;Rx/Tx
    [grpConfigCh1];cboStopBitsCh1;1
    [grpConfigCh1];cboParityCh1;None
    [grpConfigCh1];cboDataBitsCh1;8
    [grpConfigCh1];cboBaudRateCh1;9600
    [grpConfigCh1];cboComPortCh1;Com6
    [grpConfigCh1];cboTypeCh1;RS232
    [grpConfigCh1];txtNameCh1;BP Platform 1
    [grpConfigCh8];txtTCPIPPortCh2;
    [grpConfigCh8];txtIPAddressCh2;
    [grpConfigCh8];cboModeCh2;Rx
    [grpConfigCh8];cboStopBitsCh2;1
    [grpConfigCh8];cboParityCh2;None
    [grpConfigCh8];cboDataBitsCh2;8
    [grpConfigCh8];cboBaudRateCh2;9600
    [grpConfigCh8];cboComPortCh2;Com1
    [grpConfigCh8];cboTypeCh2;RS232
    [grpConfigCh8];txtNameCh2;Pemex 23

    Just want to write to debug.print in this format. The first field in the output is the channel number and comes from [grpConfigCh1] and [grpConfigCh8]. ChX can be 1 to whatever. Just a sample
    1, DBNull.Value, DBNull.Value, "Rx/Tx", 1, "None", 8, 9600, "Com6", "RS232", "BP Platform 1"
    8, DBNull.Value, DBNull.Value, "Rx", 1, "None", 8, 9600, "Com1", "RS232", "Pemex 23"
    Last edited by djsnts; Feb 27th, 2015 at 04:34 PM.

  17. #17

    Thread Starter
    Lively Member
    Join Date
    Dec 2014
    Posts
    82

    Re: How to read a formatted text file and group

    grouping on [grpConfigCh1] , [grpConfigCh8] in this case.

  18. #18
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,479

    Re: How to read a formatted text file and group

    I'm not sure what the question is...

  19. #19
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,479

    Re: How to read a formatted text file and group

    you want to split into 2 groups, then format the values from the 3rd column of each line, into a new line?

  20. #20
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,479

    Re: How to read a formatted text file and group

    ok I got it:

    Code:
    Imports Microsoft.VisualBasic.FileIO
    
    Public Class Form1
    
        Dim newLists As New List(Of List(Of String()))
    
        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            Dim tfp As New TextFieldParser("C:\Users\Paul\Desktop\1.txt")
            tfp.Delimiters = New String() {";"}
            Dim lines As New List(Of String())
            While Not tfp.EndOfData
                lines.Add(tfp.ReadFields)
            End While
            Dim unique() As String = lines.Select(Function(l) l(0)).Distinct.ToArray
    
            For Each s As String In unique
                newLists.Add(lines.Where(Function(l) l(0) = s).ToList)
            Next
    
            For Each l As List(Of String()) In newLists
                Debug.Print(String.Join(", ", l.Select(Function(s) If(s(2) = "", "DBNull.Value", s(2))).ToArray))
            Next
    
        End Sub
    
    End Class

  21. #21

    Thread Starter
    Lively Member
    Join Date
    Dec 2014
    Posts
    82

    Re: How to read a formatted text file and group

    Works fine only thing missing is for the first field in the output it should strip the number 1 from [grpConfigCh1]

    so first line of output is
    1, DBNull.Value, DBNull.Value, "Rx/Tx", 1, "None", 8, 9600, "Com6", "RS232", "BP Platform 1" - The 1 comes from stripping it from [grpConfigCh1]

    second line of output is
    8, DBNull.Value, DBNull.Value, "Rx", 1, "None", 8, 9600, "Com1", "RS232", "Pemex 23" - The 8 comes from stripping it from [grpConfigCh8]

  22. #22
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,479

    Re: How to read a formatted text file and group

    Code:
    For Each l As List(Of String()) In newLists
        Debug.Print(String.Join(", ", New String() {l(0)(0).Replace("[grpConfigCh", "").Replace("]", "")}.Concat(l.Select(Function(s) If(s(2) = "", "DBNull.Value", s(2)))).ToArray))
    Next

  23. #23

    Thread Starter
    Lively Member
    Join Date
    Dec 2014
    Posts
    82

    Re: How to read a formatted text file and group

    Works perfectly.

    Thanks for all your help mate. I have some questions that I wanted to ask you on a couple of lines of your code and was hoping you could give me an explanation. I have some errands to run so I am leaving early. I will come into work tomorrow and post my questions about your code. Thanks again.

  24. #24

    Thread Starter
    Lively Member
    Join Date
    Dec 2014
    Posts
    82

    Re: How to read a formatted text file and group

    Will not work on a input file such as this - only interested in [grpConfig lines when reading

    Sample Input File

    [tbpCh1];tbpCh1;Channel 1 (Testing)
    [tbpCh8];tbpCh8;Channel 1 (Testing)
    [tbpCh7];tbpCh7;Channel 7
    [tbpCh6];tbpCh6;Channel 6
    [tbpCh5];tbpCh5;Channel 5
    [tbpCh4];tbpCh4;Channel 4
    [tbpCh3];tbpCh3;Channel 3
    [tbpCh2];tbpCh2;Channel 2
    [grpConfigCh1];txtTCPIPPortCh1;
    [grpConfigCh1];txtIPAddressCh1;
    [grpConfigCh1];cboModeCh1;RX
    [grpConfigCh1];cboStopBitsCh1;1
    [grpConfigCh1];cboParityCh1;None
    [grpConfigCh1];cboDataBitsCh1;8
    [grpConfigCh1];cboBaudRateCh1;9600
    [grpConfigCh1];cboComPortCh1;Com5
    [grpConfigCh1];cboTypeCh1;RS232
    [grpConfigCh1];txtNameCh1;TestCh1
    [grpConfigCh8];txtTCPIPPortCh8;
    [grpConfigCh8];txtIPAddressCh8;
    [grpConfigCh8];cboModeCh8;
    [grpConfigCh8];cboStopBitsCh8;
    [grpConfigCh8];cboParityCh8;
    [grpConfigCh8];cboDataBitsCh8;
    [grpConfigCh8];cboBaudRateCh8;
    [grpConfigCh8];cboComPortCh8;
    [grpConfigCh8];cboTypeCh8;
    [grpConfigCh8];txtNameCh8;
    [grpConfigCh7];txtTCPIPPortCh7;
    [grpConfigCh7];txtIPAddressCh7;
    [grpConfigCh7];cboModeCh7;
    [grpConfigCh7];cboStopBitsCh7;
    [grpConfigCh7];cboParityCh7;
    [grpConfigCh7];cboDataBitsCh7;
    [grpConfigCh7];cboBaudRateCh7;
    [grpConfigCh7];cboComPortCh7;
    [grpConfigCh7];cboTypeCh7;
    [grpConfigCh7];txtNameCh7;
    [grpConfigCh6];txtTCPIPPortCh6;
    [grpConfigCh6];txtIPAddressCh6;
    [grpConfigCh6];cboModeCh6;Rx
    [grpConfigCh6];cboStopBitsCh6;1
    [grpConfigCh6];cboParityCh6;None
    [grpConfigCh6];cboDataBitsCh6;8
    [grpConfigCh6];cboBaudRateCh6;9600
    [grpConfigCh6];cboComPortCh6;Com8
    [grpConfigCh6];cboTypeCh6;RS232
    [grpConfigCh6];txtNameCh6;TestCh6
    [grpConfigCh5];txtTCPIPPortCh5;
    [grpConfigCh5];txtIPAddressCh5;
    [grpConfigCh5];cboModeCh5;
    [grpConfigCh5];cboStopBitsCh5;
    [grpConfigCh5];cboParityCh5;
    [grpConfigCh5];cboDataBitsCh5;
    [grpConfigCh5];cboBaudRateCh5;
    [grpConfigCh5];cboComPortCh5;
    [grpConfigCh5];cboTypeCh5;
    [grpConfigCh5];txtNameCh5;
    [grpConfigCh4];txtTCPIPPortCh4;
    [grpConfigCh4];txtIPAddressCh4;
    [grpConfigCh4];cboModeCh4;Rx
    [grpConfigCh4];cboStopBitsCh4;1
    [grpConfigCh4];cboParityCh4;None
    [grpConfigCh4];cboDataBitsCh4;8
    [grpConfigCh4];cboBaudRateCh4;9600
    [grpConfigCh4];cboComPortCh4;Com7
    [grpConfigCh4];cboTypeCh4;RS232
    [grpConfigCh4];txtNameCh4;TestCh4
    [grpConfigCh3];txtTCPIPPortCh3;
    [grpConfigCh3];txtIPAddressCh3;
    [grpConfigCh3];cboModeCh3;Rx
    [grpConfigCh3];cboStopBitsCh3;1
    [grpConfigCh3];cboParityCh3;None
    [grpConfigCh3];cboDataBitsCh3;8
    [grpConfigCh3];cboBaudRateCh3;9600
    [grpConfigCh3];cboComPortCh3;Com6
    [grpConfigCh3];cboTypeCh3;RS232
    [grpConfigCh3];txtNameCh3;TestCh3
    [grpConfigCh2];txtTCPIPPortCh2;
    [grpConfigCh2];txtIPAddressCh2;
    [grpConfigCh2];cboModeCh2;
    [grpConfigCh2];cboStopBitsCh2;
    [grpConfigCh2];cboParityCh2;
    [grpConfigCh2];cboDataBitsCh2;
    [grpConfigCh2];cboBaudRateCh2;
    [grpConfigCh2];cboComPortCh2;
    [grpConfigCh2];cboTypeCh2;
    [grpConfigCh2];txtNameCh2;
    [dgvCh1Rec1];False;;1;0101;WID;Well Identifier;;;;;;
    [dgvCh1Rec1];False;;2;0102;SKNO;Sidetrack/Hole Section No.;;;;;;
    [dgvCh1Rec1];False;;3;0103;RID;Record Identifier;;;;;;
    [dgvCh1Rec1];False;;4;0104;SQID;Sequence Identifier;;;;;;
    [dgvCh1Rec1];False;;5;0105;DATE;Date;;;;;;
    [dgvCh1Rec1];False;;6;0106;TIME;Time;;;;;;
    [dgvCh1Rec1];False;;7;0107;ACTC;Activity Code;;;;;;
    [dgvCh1Rec1];True;Rx;8;0108;DBTM;Depth Bit - Meas;;;;;;
    [dgvCh1Rec1];False;;9;0109;DBTV;Depth Bit - Vert;;;;;;
    [dgvCh1Rec1];True;Rx;10;0110;DMEA;Depth Hole - Meas;;;;;;
    [dgvCh1Rec1];False;;11;0111;DVER;Depth Hole - Vert;;;;;;
    [dgvCh1Rec1];True;Rx;12;0112;BPOS;Block Position;;;;;;
    [dgvCh1Rec1];True;Rx;13;0113;ROPA;Rate of Penetration - Avg;;;;;;
    [dgvCh1Rec1];True;Rx;14;0114;HKLA;Hookload - Avg;;;;;;
    [dgvCh1Rec1];False;;15;0115;HKLX;Hookload - Max;;;;;;
    [dgvCh1Rec1];True;Rx;16;0116;WOBA;Weight on Bit - Surface, Avg;;;;;;
    [dgvCh1Rec1];False;;17;0117;WOBX;Weight on Bit - Surface, Max;;;;;;
    [dgvCh1Rec1];True;Rx;18;0118;TQA;Rotary Torque - Surface, Avg;;;;;;
    [dgvCh1Rec1];False;;19;0119;TQX;Rotary Torque - Surface, Max;;;;;;
    [dgvCh1Rec1];True;Rx;20;0120;RPMA;Rotary Speed - Surface, Avg;;;;;;
    [dgvCh1Rec1];True;Rx;21;0121;SPPA;Stand Pipe Pressure - Avg;;;;;;
    [dgvCh1Rec1];False;;22;0122;CHKP;Casing (Choke) Pressure - Avg;;;;;;
    [dgvCh1Rec1];True;Rx;23;0123;SPM1;Pump Stroke Rate #1 - Avg;;;;;;
    [dgvCh1Rec1];True;Rx;24;0124;SPM2;Pump Stroke Rate #2 - Avg;;;;;;
    [dgvCh1Rec1];True;Rx;25;0125;SPM3;Pump Stroke Rate #3 - Avg;;;;;;
    [dgvCh1Rec1];False;;26;0126;TVA;Tank Volume (active) - Avg;;;;;;
    [dgvCh1Rec1];False;;27;0127;TVCA;Tank Volume Change (active);;;;;;
    [dgvCh1Rec1];False;;28;0128;MFOP;Mud Flow Out % - Avg;;;;;;
    [dgvCh1Rec1];False;;29;0129;MFOA;Mud Flow Out - Avg;;;;;;
    [dgvCh1Rec1];False;;30;0130;MFIA;Mud Flow In - Avg;;;;;;
    [dgvCh1Rec1];False;;31;0131;MDOA;Mud Density Out - Avg;;;;;;
    [dgvCh1Rec1];False;;32;0132;MDIA;Mud Density In - Avg;;;;;;
    [dgvCh1Rec1];False;;33;0133;MTOA;Mud Temperature Out - Avg;;;;;;
    [dgvCh1Rec1];False;;34;0134;MTIA;Mud Temperature In - Avg;;;;;;
    [dgvCh1Rec1];False;;35;0135;MCOA;Mud Conductivity Out - Avg;;;;;;
    [dgvCh1Rec1];False;;36;0136;MCIA;Mud Conductivity In - Avg;;;;;;
    [dgvCh1Rec1];False;;37;0137;STKC;Pump Stroke Count - Cumulative;;;;;;
    [dgvCh1Rec1];False;;38;0138;LSTK;Lag Strokes;;;;;;
    [dgvCh1Rec1];True;Tx;39;0139;DRTM;Depth Returns - Meas;;;;;;
    [dgvCh1Rec1];True;Tx;40;0140;GASA;Gas - Avg;;;;;;
    [dgvCh1Rec1];False;;41;0141;SPR1;<SPARE 1>;;;;;;
    [dgvCh1Rec1];False;;42;0142;SPR2;<SPARE 2>;;;;;;
    [dgvCh1Rec1];False;;43;0143;SPR3;<SPARE 3>;;;;;;
    [dgvCh1Rec1];False;;44;0144;SPR4;<SPARE 4>;;;;;;
    [dgvCh1Rec1];False;;45;0145;SPR5;<SPARE 5>;;;;;;

  25. #25
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,479

    Re: How to read a formatted text file and group

    What is the error you are getting?

  26. #26

    Thread Starter
    Lively Member
    Join Date
    Dec 2014
    Posts
    82

    Re: How to read a formatted text file and group

    using my last post as a sample input file, it should only grab lines that start with [grpConfig and write them to a datatable. Previously I said debug.print

    Result in datatable

    1, DBNull.Value, DBNull.Value, RX, 1, None, 8, 9600, Com5, RS232, TestCh1
    8, DBNull.Value, DBNull.Value, DBNull.Value, DBNull.Value, DBNull.Value, DBNull.Value, DBNull.Value, DBNull.Value, DBNull.Value, DBNull.Value
    7, DBNull.Value, DBNull.Value, DBNull.Value, DBNull.Value, DBNull.Value, DBNull.Value, DBNull.Value, DBNull.Value, DBNull.Value, DBNull.Value
    6, DBNull.Value, DBNull.Value, Rx, 1, None, 8, 9600, Com8, RS232, TestCh6
    5, DBNull.Value, DBNull.Value, DBNull.Value, DBNull.Value, DBNull.Value, DBNull.Value, DBNull.Value, DBNull.Value, DBNull.Value, DBNull.Value
    4, DBNull.Value, DBNull.Value, Rx, 1, None, 8, 9600, Com7, RS232, TestCh4
    3, DBNull.Value, DBNull.Value, Rx, 1, None, 8, 9600, Com6, RS232, TestCh3
    2, DBNull.Value, DBNull.Value, DBNull.Value, DBNull.Value, DBNull.Value, DBNull.Value, DBNull.Value, DBNull.Value, DBNull.Value, DBNull.Value

  27. #27

    Thread Starter
    Lively Member
    Join Date
    Dec 2014
    Posts
    82

    Re: How to read a formatted text file and group

    No syntax errors just logical error with input file. Should ignore any lines not starting with [grpConfig

  28. #28
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,479

    Re: How to read a formatted text file and group

    For those lines:

    Code:
    Imports Microsoft.VisualBasic.FileIO
    
    Public Class Form1
    
        Dim newLists As New List(Of List(Of String()))
    
        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            Dim tfp As New TextFieldParser("C:\Users\Paul\Desktop\1.txt")
            tfp.Delimiters = New String() {";"}
            Dim lines As New List(Of String())
    
            Dim c1 As Integer = 0
    
            While Not tfp.EndOfData
                Dim fields() As String = tfp.ReadFields
                lines.Add(fields)
                If fields(0).StartsWith("[grpConfigCh") Then c1 += 1
            End While
    
            Dim unique() As String = lines.Select(Function(l) l(0)).Distinct.ToArray
    
            For Each s As String In unique
                newLists.Add(lines.Where(Function(l) l(0) = s).ToList)
            Next
    
            Dim c2 As Integer = 0
    
            For Each l As List(Of String()) In newLists
                For Each a As String() In l
                    If a(0).StartsWith("[grpConfigCh") Then
                        Debug.Print(String.Join(", ", New String() {l(0)(0).Replace("[grpConfigCh", "").Replace("]", "")}.Concat(l.Select(Function(s) If(s(2) = "", "DBNull.Value", s(2)))).ToArray))
                        c2 += 1
                    End If
                Next
            Next
    
            MsgBox(String.Format("c1: {0}{3}c2: {1}{3}c1 = c2 = {2}", c1, c2, c1 = c2, Environment.NewLine))
        End Sub
    
    End Class

  29. #29

    Thread Starter
    Lively Member
    Join Date
    Dec 2014
    Posts
    82

    Re: How to read a formatted text file and group

    I have attached the input file. The code generates each output line 10x for some reason. Should be once.

    1, DBNull.Value, DBNull.Value, RX, 1, None, 8, 9600, Com5, RS232, TestCh1
    1, DBNull.Value, DBNull.Value, RX, 1, None, 8, 9600, Com5, RS232, TestCh1
    1, DBNull.Value, DBNull.Value, RX, 1, None, 8, 9600, Com5, RS232, TestCh1
    1, DBNull.Value, DBNull.Value, RX, 1, None, 8, 9600, Com5, RS232, TestCh1
    1, DBNull.Value, DBNull.Value, RX, 1, None, 8, 9600, Com5, RS232, TestCh1
    1, DBNull.Value, DBNull.Value, RX, 1, None, 8, 9600, Com5, RS232, TestCh1
    1, DBNull.Value, DBNull.Value, RX, 1, None, 8, 9600, Com5, RS232, TestCh1
    1, DBNull.Value, DBNull.Value, RX, 1, None, 8, 9600, Com5, RS232, TestCh1
    1, DBNull.Value, DBNull.Value, RX, 1, None, 8, 9600, Com5, RS232, TestCh1
    1, DBNull.Value, DBNull.Value, RX, 1, None, 8, 9600, Com5, RS232, TestCh1
    8, DBNull.Value, DBNull.Value, DBNull.Value, DBNull.Value, DBNull.Value, DBNull.Value, DBNull.Value, DBNull.Value, DBNull.Value, DBNull.Value
    8, DBNull.Value, DBNull.Value, DBNull.Value, DBNull.Value, DBNull.Value, DBNull.Value, DBNull.Value, DBNull.Value, DBNull.Value, DBNull.Value
    8, DBNull.Value, DBNull.Value, DBNull.Value, DBNull.Value, DBNull.Value, DBNull.Value, DBNull.Value, DBNull.Value, DBNull.Value, DBNull.Value
    8, DBNull.Value, DBNull.Value, DBNull.Value, DBNull.Value, DBNull.Value, DBNull.Value, DBNull.Value, DBNull.Value, DBNull.Value, DBNull.Value
    8, DBNull.Value, DBNull.Value, DBNull.Value, DBNull.Value, DBNull.Value, DBNull.Value, DBNull.Value, DBNull.Value, DBNull.Value, DBNull.Value
    8, DBNull.Value, DBNull.Value, DBNull.Value, DBNull.Value, DBNull.Value, DBNull.Value, DBNull.Value, DBNull.Value, DBNull.Value, DBNull.Value
    8, DBNull.Value, DBNull.Value, DBNull.Value, DBNull.Value, DBNull.Value, DBNull.Value, DBNull.Value, DBNull.Value, DBNull.Value, DBNull.Value
    8, DBNull.Value, DBNull.Value, DBNull.Value, DBNull.Value, DBNull.Value, DBNull.Value, DBNull.Value, DBNull.Value, DBNull.Value, DBNull.Value
    8, DBNull.Value, DBNull.Value, DBNull.Value, DBNull.Value, DBNull.Value, DBNull.Value, DBNull.Value, DBNull.Value, DBNull.Value, DBNull.Value
    8, DBNull.Value, DBNull.Value, DBNull.Value, DBNull.Value, DBNull.Value, DBNull.Value, DBNull.Value, DBNull.Value, DBNull.Value, DBNull.Value
    7, DBNull.Value, DBNull.Value, DBNull.Value, DBNull.Value, DBNull.Value, DBNull.Value, DBNull.Value, DBNull.Value, DBNull.Value, DBNull.Value
    7, DBNull.Value, DBNull.Value, DBNull.Value, DBNull.Value, DBNull.Value, DBNull.Value, DBNull.Value, DBNull.Value, DBNull.Value, DBNull.Value
    7, DBNull.Value, DBNull.Value, DBNull.Value, DBNull.Value, DBNull.Value, DBNull.Value, DBNull.Value, DBNull.Value, DBNull.Value, DBNull.Value
    7, DBNull.Value, DBNull.Value, DBNull.Value, DBNull.Value, DBNull.Value, DBNull.Value, DBNull.Value, DBNull.Value, DBNull.Value, DBNull.Value
    7, DBNull.Value, DBNull.Value, DBNull.Value, DBNull.Value, DBNull.Value, DBNull.Value, DBNull.Value, DBNull.Value, DBNull.Value, DBNull.Value
    7, DBNull.Value, DBNull.Value, DBNull.Value, DBNull.Value, DBNull.Value, DBNull.Value, DBNull.Value, DBNull.Value, DBNull.Value, DBNull.Value
    7, DBNull.Value, DBNull.Value, DBNull.Value, DBNull.Value, DBNull.Value, DBNull.Value, DBNull.Value, DBNull.Value, DBNull.Value, DBNull.Value
    7, DBNull.Value, DBNull.Value, DBNull.Value, DBNull.Value, DBNull.Value, DBNull.Value, DBNull.Value, DBNull.Value, DBNull.Value, DBNull.Value
    7, DBNull.Value, DBNull.Value, DBNull.Value, DBNull.Value, DBNull.Value, DBNull.Value, DBNull.Value, DBNull.Value, DBNull.Value, DBNull.Value
    7, DBNull.Value, DBNull.Value, DBNull.Value, DBNull.Value, DBNull.Value, DBNull.Value, DBNull.Value, DBNull.Value, DBNull.Value, DBNull.Value
    6, DBNull.Value, DBNull.Value, Rx, 1, None, 8, 9600, Com8, RS232, TestCh6
    6, DBNull.Value, DBNull.Value, Rx, 1, None, 8, 9600, Com8, RS232, TestCh6
    6, DBNull.Value, DBNull.Value, Rx, 1, None, 8, 9600, Com8, RS232, TestCh6
    6, DBNull.Value, DBNull.Value, Rx, 1, None, 8, 9600, Com8, RS232, TestCh6
    6, DBNull.Value, DBNull.Value, Rx, 1, None, 8, 9600, Com8, RS232, TestCh6
    6, DBNull.Value, DBNull.Value, Rx, 1, None, 8, 9600, Com8, RS232, TestCh6
    6, DBNull.Value, DBNull.Value, Rx, 1, None, 8, 9600, Com8, RS232, TestCh6
    6, DBNull.Value, DBNull.Value, Rx, 1, None, 8, 9600, Com8, RS232, TestCh6
    6, DBNull.Value, DBNull.Value, Rx, 1, None, 8, 9600, Com8, RS232, TestCh6
    6, DBNull.Value, DBNull.Value, Rx, 1, None, 8, 9600, Com8, RS232, TestCh6
    5, DBNull.Value, DBNull.Value, DBNull.Value, DBNull.Value, DBNull.Value, DBNull.Value, DBNull.Value, DBNull.Value, DBNull.Value, DBNull.Value
    5, DBNull.Value, DBNull.Value, DBNull.Value, DBNull.Value, DBNull.Value, DBNull.Value, DBNull.Value, DBNull.Value, DBNull.Value, DBNull.Value
    5, DBNull.Value, DBNull.Value, DBNull.Value, DBNull.Value, DBNull.Value, DBNull.Value, DBNull.Value, DBNull.Value, DBNull.Value, DBNull.Value
    5, DBNull.Value, DBNull.Value, DBNull.Value, DBNull.Value, DBNull.Value, DBNull.Value, DBNull.Value, DBNull.Value, DBNull.Value, DBNull.Value
    5, DBNull.Value, DBNull.Value, DBNull.Value, DBNull.Value, DBNull.Value, DBNull.Value, DBNull.Value, DBNull.Value, DBNull.Value, DBNull.Value
    5, DBNull.Value, DBNull.Value, DBNull.Value, DBNull.Value, DBNull.Value, DBNull.Value, DBNull.Value, DBNull.Value, DBNull.Value, DBNull.Value
    5, DBNull.Value, DBNull.Value, DBNull.Value, DBNull.Value, DBNull.Value, DBNull.Value, DBNull.Value, DBNull.Value, DBNull.Value, DBNull.Value
    5, DBNull.Value, DBNull.Value, DBNull.Value, DBNull.Value, DBNull.Value, DBNull.Value, DBNull.Value, DBNull.Value, DBNull.Value, DBNull.Value
    5, DBNull.Value, DBNull.Value, DBNull.Value, DBNull.Value, DBNull.Value, DBNull.Value, DBNull.Value, DBNull.Value, DBNull.Value, DBNull.Value
    5, DBNull.Value, DBNull.Value, DBNull.Value, DBNull.Value, DBNull.Value, DBNull.Value, DBNull.Value, DBNull.Value, DBNull.Value, DBNull.Value
    4, DBNull.Value, DBNull.Value, Rx, 1, None, 8, 9600, Com7, RS232, TestCh4
    4, DBNull.Value, DBNull.Value, Rx, 1, None, 8, 9600, Com7, RS232, TestCh4
    4, DBNull.Value, DBNull.Value, Rx, 1, None, 8, 9600, Com7, RS232, TestCh4
    4, DBNull.Value, DBNull.Value, Rx, 1, None, 8, 9600, Com7, RS232, TestCh4
    4, DBNull.Value, DBNull.Value, Rx, 1, None, 8, 9600, Com7, RS232, TestCh4
    4, DBNull.Value, DBNull.Value, Rx, 1, None, 8, 9600, Com7, RS232, TestCh4
    4, DBNull.Value, DBNull.Value, Rx, 1, None, 8, 9600, Com7, RS232, TestCh4
    4, DBNull.Value, DBNull.Value, Rx, 1, None, 8, 9600, Com7, RS232, TestCh4
    4, DBNull.Value, DBNull.Value, Rx, 1, None, 8, 9600, Com7, RS232, TestCh4
    4, DBNull.Value, DBNull.Value, Rx, 1, None, 8, 9600, Com7, RS232, TestCh4
    3, DBNull.Value, DBNull.Value, Rx, 1, None, 8, 9600, Com6, RS232, TestCh3
    3, DBNull.Value, DBNull.Value, Rx, 1, None, 8, 9600, Com6, RS232, TestCh3
    3, DBNull.Value, DBNull.Value, Rx, 1, None, 8, 9600, Com6, RS232, TestCh3
    3, DBNull.Value, DBNull.Value, Rx, 1, None, 8, 9600, Com6, RS232, TestCh3
    3, DBNull.Value, DBNull.Value, Rx, 1, None, 8, 9600, Com6, RS232, TestCh3
    3, DBNull.Value, DBNull.Value, Rx, 1, None, 8, 9600, Com6, RS232, TestCh3
    3, DBNull.Value, DBNull.Value, Rx, 1, None, 8, 9600, Com6, RS232, TestCh3
    3, DBNull.Value, DBNull.Value, Rx, 1, None, 8, 9600, Com6, RS232, TestCh3
    3, DBNull.Value, DBNull.Value, Rx, 1, None, 8, 9600, Com6, RS232, TestCh3
    3, DBNull.Value, DBNull.Value, Rx, 1, None, 8, 9600, Com6, RS232, TestCh3
    2, DBNull.Value, DBNull.Value, DBNull.Value, DBNull.Value, DBNull.Value, DBNull.Value, DBNull.Value, DBNull.Value, DBNull.Value, DBNull.Value
    2, DBNull.Value, DBNull.Value, DBNull.Value, DBNull.Value, DBNull.Value, DBNull.Value, DBNull.Value, DBNull.Value, DBNull.Value, DBNull.Value
    2, DBNull.Value, DBNull.Value, DBNull.Value, DBNull.Value, DBNull.Value, DBNull.Value, DBNull.Value, DBNull.Value, DBNull.Value, DBNull.Value
    2, DBNull.Value, DBNull.Value, DBNull.Value, DBNull.Value, DBNull.Value, DBNull.Value, DBNull.Value, DBNull.Value, DBNull.Value, DBNull.Value
    2, DBNull.Value, DBNull.Value, DBNull.Value, DBNull.Value, DBNull.Value, DBNull.Value, DBNull.Value, DBNull.Value, DBNull.Value, DBNull.Value
    2, DBNull.Value, DBNull.Value, DBNull.Value, DBNull.Value, DBNull.Value, DBNull.Value, DBNull.Value, DBNull.Value, DBNull.Value, DBNull.Value
    2, DBNull.Value, DBNull.Value, DBNull.Value, DBNull.Value, DBNull.Value, DBNull.Value, DBNull.Value, DBNull.Value, DBNull.Value, DBNull.Value
    2, DBNull.Value, DBNull.Value, DBNull.Value, DBNull.Value, DBNull.Value, DBNull.Value, DBNull.Value, DBNull.Value, DBNull.Value, DBNull.Value
    2, DBNull.Value, DBNull.Value, DBNull.Value, DBNull.Value, DBNull.Value, DBNull.Value, DBNull.Value, DBNull.Value, DBNull.Value, DBNull.Value
    2, DBNull.Value, DBNull.Value, DBNull.Value, DBNull.Value, DBNull.Value, DBNull.Value, DBNull.Value, DBNull.Value, DBNull.Value, DBNull.Value
    Attached Files Attached Files

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width