Click here to Skip to main content
15,895,142 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
Hi,
I want to get some columns from text file with 7800 columns. the columns that I search about in this text file have the same header. the program that I have written in C# is as following:
C#
  int frcount;
  int fccount=7800;

  string fpkm_path = @"inputfile.txt";

  List<int> list = new List<int>();

  frcount = File.ReadLines(fpkm_path).Count();

  string[,] fpkm_out = new string[frcount, fccount];
  string out_txt = @"outputfile.txt";
  string ex = "column_header";
 try
  {
              fpkm_out = txt_TO_arr(fpkm_path, frcount, fccount);
              list = No_in_List(ex, fpkm_out);
              List_TO_Text(fpkm_out, list, out_txt);

  }
tb.Text = "Done!!";

The function that I have used are as following:
1- txt_TO_arr: this will convert the text file to an array:
C#
public string[,] txt_TO_arr(string txtfile,int rcount,int ccount)     //////////////////////////////////// Put text file contents in an array
       {
           string[,] arr_in = new string[rcount, ccount];
           // Save this text file in an array
           // Read the file and display it line by line.
           using (StreamReader line_file = new StreamReader(txtfile))
           {

               int i = 0;
               string line;
               while ((line = line_file.ReadLine()) != null)
               {
                   int j = 0;
                   var delimiters = new char[] { '\t' };
                   var segments = line.Split(delimiters, StringSplitOptions.RemoveEmptyEntries);

                   foreach (var segment in segments)
                   {
                       arr_in[i, j] = segment;
                       j = j + 1;
                   }
                   i = i + 1;
               }

               line_file.Close();
           }
           return arr_in;
       }


2- No_in_List: which will create a list contain the position of the column that has the header that I am looking fore

C#
public List<int> No_in_List(string id, string[,] fpkm_arr)//Put col.s number in a list
       {

           List<int> list = new List<int>();
           list.Add(0);
           for (int f = 1; f < fpkm_arr.GetLength(1); f++)
               {
                   if (id == fpkm_arr[0, f])
                   {
                       list.Add(f);

                   }

               }

             return list;

        }


3- List_TO_Text: which will create a text file that contain only the columns that have the header that I want
C#
public void List_TO_Text(string[,] arr, List<int> list, string fileName)        /////////////// Put columns in a text file
        {
            StreamWriter tw = new StreamWriter(fileName);
          
                for (int i = 0; i < arr.GetLength(0); i++)
                {
                    foreach (int item in list)
                    {
                            tw.Write(arr[i,item] + '\t');
                   }
                    tw.WriteLine();
              }
                tw.Close(); 
        }


This script work fine with smaller text files like contain 1000 columns but don't get any result when trying to run it will my file with 7800 columns.

if anyone can help I will be very thankful.

Thank you.
Posted
Updated 11-Apr-15 22:36pm
v2
Comments
Maciej Los 11-Apr-15 14:54pm    
How this XML look like?
Member 11290013 11-Apr-15 15:09pm    
I am sorry some thing went wrong with the title of the question. what do I want is to extract columns from text file that have the same column header and write them in another text file.
Maciej Los 12-Apr-15 5:37am    
How your text file looks like?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900