I am trying to remove all the columns after the column name "LAYER"

My input text files have a more than 10 columns which are tab delimited

Designator MAX PN Footprint Center-X(mm) Center-Y(mm) Layer Fitted Orentation Design level
So i want to delete all the columns and the contents after the column name Layer. i tried a couple of ways and i figured out a way.. in which i am able to delete the last column contents. But in additional to that my header columns is getting DELETED. IS there any way i can retain my first siz header columns same as my contents.. Any helps.

Codes:

public void column_delete()
    {
        string old;
        string outputText = String.Empty;

        StringBuilder sb = new StringBuilder();
        using (StreamReader sr = File.OpenText(textBox1.Text))
        {
            while ((old = sr.ReadLine()) != null)
            {
                //var line = sr.ReadLine();
                var value = old.Split('\t');
                // Ignore first line (headers)
                if (!old.Contains("Designator"))
                {
                    // Split by \t (tab) and remove blank space
                    var tokens = value.Select(x => x.Trim());

                    // Take first 6 tokens (0 to 5)
                    var newTokens = tokens.Take(6);

                    // my top header column is getting deleted
                    outputText += String.Join("\t", newTokens) + Environment.NewLine;
                }
            }
        }
        System.IO.File.WriteAllText(textBox1.Text, outputText);
    }

My input text file

 Designator MAX PN  Footprint   Center-X(mm)    Center-Y(mm)    Layer   Fitted

   C71  100-0042    C1206   166.116 12.7    Bottom  Fitted
   C160 100-0119    C1206   15.494  215.138 Bottom  NotFitted
   C67  100-0119    zebra   15.748  191.008 Bottom  Fitted
   C68  100-0119    zebra   14.732  199.644 Bottom  Fitted  
   C70  100-0119    C1206   15.748  208.153 Bottom  Fitted  
   AC1  100-0177    C1206   172.72  28.956  Bottom  Fitted  
   AC2  100-0177    C1206   164.592 28.956  Bottom  Fitted
   CR4  100-0268    C1206 - HELLO   164.592 91.44   Bottom  Fitted  
   CR5  100-0268    0805M - CAPACITOR   134.112 91.44   Bottom  Fitted  
   CR7  100-0268    C1206   150.368 91.44   Bottom  Fitted  
   C41  100-0283    CAP_D   115.306 40.132  Bottom  Fitted  
   C47  100-0283    CAP_D   114.798 32.004  Bottom  Fitted

I am getting the output text file as below.. My output text file is not having header's once its processed

 C71    100-0042    C1206   166.116 12.7    Bottom
 C160   100-0119    C1206   15.494  215.138 Bottom
 C67    100-0119    zebra   15.748  191.008 Bottom
 C68    100-0119    zebra   14.732  199.644 Bottom
 C70    100-0119    C1206   15.748  208.153 Bottom
 AC1    100-0177    C1206   172.72  28.956  Bottom
 AC2    100-0177    C1206   164.592 28.956  Bottom
 CR4    100-0268    C1206 - HELLO   164.592 91.44   Bottom
 CR5    100-0268    0805M - CAPACITOR   134.112 91.44   Bottom
 CR7    100-0268    C1206   150.368 91.44   Bottom
 C41    100-0283    CAP_D   115.306 40.132  Bottom
 C47    100-0283    CAP_D   114.798 32.004  Bottom

As you can see above **my first six header column names is getting deleted... **

Since if (!old.Contains("Designator")) will skip the header line it will not be included in the new output that you build in outputText += String.Join("\t", newTokens) + Environment.NewLine;.

You could add it separately in the else, something like:

 if (!old.Contains("Designator")){

    // Split by \t (tab) and remove blank space
    var tokens = value.Select(x => x.Trim());

    // Take first 6 tokens (0 to 5)
    var newTokens = tokens.Take(6);

    // my top header column is getting deleted
    outputText += String.Join("\t", newTokens) + Environment.NewLine;
}else{
    // append header
    outputText += old + Environment.NewLine;
}

Although I'd think that if you are removing the entire column you might as well remove the header. Then there wouldn't be a need to handle the header differently, just have its last column deleted as well.

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.