0

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. 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
Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129

1 Answers1

0

If I got this right, you want to take the first 6 items of each line but the first, correct?

        string inputText =
            @"Designator    MAX PN  Footprint   Center-X(mm)    Center-Y(mm)    Layer   Fitted  Orentation  Designator  level
            hi  hi  hi  hi  hi  hi  remove  remove  remove  remove ";

        string outputText = @"Designator    MAX PN  Footprint   Center-X(mm)    Center-Y(mm)    Layer   Fitted  Orentation  Designator  level\n";

        // Reading line by line to simulate a text file
        foreach (var line in inputText.Split('\n'))
        {
            // Ignore first line (headers)
            if (!line.Contains("Designator"))
            {
                // Split by \t (tab) and remove blank space
                var tokens = line.Split('\t').Select(x => x.Trim());

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

                // Join the new tokens into a new string (you would want to write this to your file...)
                outputText += String.Join("", newTokens) + Environment.NewLine;
            }
        }
Rodrigo Silva
  • 432
  • 1
  • 6
  • 18
  • thanks for reply..while converting my header text is getting deleted `Designator MAX PN Footprint Center-X(mm) Center-Y(mm) Layer` –  Dec 22 '14 at 04:36
  • Check the updated answer, just initialize the outputText variable with the headers instead of an empty string... – Rodrigo Silva Dec 22 '14 at 13:19
  • @ Rodrigo Silva yea in that case what happens user creates only an input text file with first five columns only. that time this layer column will come there simply.. So that why i dint do that.. if the input text file has layer then i need that column and contents. if not.. according to the user input file –  Dec 22 '14 at 13:53
  • 1
    If you want the user to be able to choose the columns you could associate the columns to a fixed index, split each content line by "\t" and choose accordingly. – Rodrigo Silva Dec 22 '14 at 14:40
  • you are free to edit my code.. I tried and i could find that.. each time when i split by '`\t'` my output file seems to be empty –  Dec 22 '14 at 14:46