Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Removing the first line of a text file in C#

Tags:

c#

.net

windows

I can currently remove the last line of a text file using:

    var lines = System.IO.File.ReadAllLines("test.txt");
    System.IO.File.WriteAllLines("test.txt", lines.Take(lines.Length - 1).ToArray());

Although, how is it possible to instead remove the beginning of the text file?

like image 263
Johnathan Middleton Avatar asked Aug 10 '11 09:08

Johnathan Middleton


People also ask

How do I show the first line of a text file?

The head command is used to display the beginning of a text file or piped data. By default, it displays the first ten lines of the specified files. The tail command is also used to display the ending part of the file.

How do I remove the first line of a text file in Unix?

Using the sed Command Removing the first line from an input file using the sed command is pretty straightforward. The sed command in the example above isn't hard to understand. The parameter '1d' tells the sed command to apply the 'd' (delete) action on line number '1'.


2 Answers

Instead of lines.Take, you can use lines.Skip, like:

var lines = File.ReadAllLines("test.txt");
File.WriteAllLines("test.txt", lines.Skip(1).ToArray());

to truncate at the beginning despite the fact that the technique used (read all text and write everything back) is very inefficient.

About the efficient way: The inefficiency comes from the necessity to read the whole file into memory. The other way around could easily be to seek in a stream and copy the stream to another output file, delete the original, and rename the old. That one would be equally fast and yet consume much less memory.

Truncating a file at the end is much easier. You can just find the trunaction position and call FileStream.SetLength().

like image 186
Sedat Kapanoglu Avatar answered Oct 31 '22 18:10

Sedat Kapanoglu


Here is an alternative:

        using (var stream = File.OpenRead("C:\\yourfile"))
        {
            var items = new LinkedList<string>();
            using (var reader = new StreamReader(stream))
            {
                reader.ReadLine(); // skip one line
                string line;
                while ((line = reader.ReadLine()) != null)
                {
                    //it's far better to do the actual processing here
                    items.AddLast(line);
                }
            }
        }

Update

If you need an IEnumerable<string> and don't want to waste memory you could do something like this:

    public static IEnumerable<string> GetFileLines(string filename)
    {
        using (var stream = File.OpenRead(filename))
        {
            using (var reader = new StreamReader(stream))
            {
                reader.ReadLine(); // skip one line
                string line;
                while ((line = reader.ReadLine()) != null)
                {
                    yield return line;
                }
            }
        }
    }


    static void Main(string[] args)
    {
        foreach (var line in GetFileLines("C:\\yourfile.txt"))
        {
            // do something with the line here.
        }
    }
like image 25
jgauffin Avatar answered Oct 31 '22 17:10

jgauffin