0

I have a text file with 7 number of columns. The columns are always same may be 7 or more sometimes. My text file is tab delimited. So i would like to know how can i set a search for all the columns that are available in my text file and delete that entire line.

Currently, i am writing a single function for each columns search and delete the file. i need a common function to search the whole text file and append the line

Codes:

 public void stack()
    {
        string old;
        string iniPath = Application.StartupPath + "\\list.ini";
        bool isDeleteSectionFound = false;
        List<string> deleteCodeList = new List<string>();
        using (StreamReader sr = File.OpenText(iniPath))
        {
            while ((old = sr.ReadLine()) != null)
            {
                if (old.Trim().Equals("[DELETE]"))
                {
                    isDeleteSectionFound = true;
                }
                if (isDeleteSectionFound && !old.Trim().Equals("[DELETE]"))
                {
                    deleteCodeList.Add(old.Trim());
                }
            }
        }

        StringBuilder sb = new StringBuilder();
        using (var reader = new StreamReader(File.OpenRead(textBox1.Text)))
        {
            //;

            List<string> data = new List<string>();
            while (!reader.EndOfStream)
            {
                var line = reader.ReadLine();
                var values = line.Split('\t');

                if (values.Contains(deleteCodeList))// getting that error
                    continue;

                //
                data.Add(string.Join("\t", values));

            }
        }
        File.WriteAllText(textBox1.Text, sb.ToString());
    }

1 Answers1

0

This is a simple application Console that read a file split the line and search for a string. If a string is found then continue else add the line to an array.

using System.IO;
using System.Linq;

static void Main(string[] args)
{
     var reader = new StreamReader(File.OpenRead(@"C:\test.ini"));

        List<string> data = new List<string>();
        while (!reader.EndOfStream)
        {
            var line = reader.ReadLine();
            var values = line.Split('\t');

            if (values.Contains("REMOVE"))
                continue;

            //
            data.Add(string.Join("\t",values));

        }

        //Inside data you have all the lines without the removed. You can rewrite
        //to other file 


}

The snippet for open and read the file is taken from this question

Test File :

223431  121232112   13122   TEST
322323  1232332 1233123 2311231 123121
232323  REMOVE  123121  12123211    121212
Community
  • 1
  • 1
2GDev
  • 2,478
  • 1
  • 20
  • 32
  • 1
    i tried ur code and it was beautiful.. i have one small doubt.. i am adding a list of files from my .ini file.. and i want to compare that list with ur code. When i gave my codes `deleteCodeList` its showing an error called `'string[]' does not contain a definition for 'Contains' and the best extension method overload 'System.Linq.Queryable.Contains(System.Linq.IQueryable, TSource)' has some invalid arguments` –  Dec 19 '14 at 16:38
  • 1
    try to add using System.Linq – 2GDev Dec 19 '14 at 16:43
  • 1
    in my code i'm searching for a string ("DELETE") you are using a List – 2GDev Dec 19 '14 at 16:57