Aidan
Aidan

Reputation: 317

Deleting text from text file

I want to know how I can delete a certain amount of text from a file on each line.

I can not think of a way of accomplishing such a task.

878     57  2
882     63  1
887     62  1
1001    71  0
1041    79  1
1046    73  2

This is what the text file looks like but I only want the numbers that are on the very left. I can not manually the 2 rows on the right because there is over 16,000 lines of this.

The numbers on the left also change in length so I can't read them by length.

I'm also not sure what character the numbers are separated by, it may be tab.

Anyone have any ideas on what I could try?

If you wish to take a look at the text file, here: http://pastebin.com/xyaCsc6W

Upvotes: 0

Views: 191

Answers (5)

Fazz
Fazz

Reputation: 15

You could also do this which will give you a list of results of only the left (column) characters (numeric/alphanumeric) of the text file:

var results = File.ReadAllLines("filename.txt")
              .Select(line => line.Split('\t').First())
              .ToList();

It looks like the text file is delimited by tabs.

To save the list of results back into a text file add the following in addition:

 File.WriteAllLines("results.txt", results.ToArray());

Upvotes: 0

Sudhakar Tillapudi
Sudhakar Tillapudi

Reputation: 26209

        StringBuilder sb = new StringBuilder();
       //read the line by line of file.txt
        using (StreamReader sr = new StreamReader("file.txt"))
        {
            String line;
            // Read and display lines from the file until the end of 
            // the file is reached.
            while ((line = sr.ReadLine()) != null)
            {
                //for each line identify the space
                //cut the data from beginning of each line to where it finds space
                string str = line.Substring(0, line.IndexOf(' '));

                //Append each modifed line into string builder object
                sb.AppendLine(str);

            }
        }

        //Create temp newfile
        using (File.Create("newfile.txt"))
        {
            //create newfile to store the modified data
        }

        //Add modified data into newfile
        File.WriteAllText("newfile.txt",sb.ToString());

        //Replace with new file
        File.Replace("newfile.txt", "file.txt", null);

Upvotes: 0

Noctis
Noctis

Reputation: 11783

You could do:

var col = 
 from s in File.ReadAllLines(input_file_name);
 select s.Split("   ".ToCharArray())[0]; 

Note: In the Split(" ") I have a space and a tab characters.

Upvotes: 0

Pascalz
Pascalz

Reputation: 2378

string line;
using (var sr = new StreamReader(@"E:\test1.txt"))
{
    using (var sw = new StreamWriter(@"E:\test1.tmp"))
    {
        while (!sr.EndOfStream)
        {
            line = sr.ReadLine();
            line = Regex.Match(line, @"([\d]*)").Groups[1].Value;
            sw.WriteLine(line);
        }
    }
}
File.Replace(@"E:\test1.tmp", @"E:\test1.txt", null);

Upvotes: 0

Sriram Sakthivel
Sriram Sakthivel

Reputation: 73502

var query = File.ReadLines("input.txt")
    .Where(x => char.IsDigit(x.FirstOrDefault()))
    .Select(x => string.Join("", x.TakeWhile(char.IsDigit)));

File.WriteAllLines("output.txt", query);

Upvotes: 3

Related Questions