Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I delete the first "X" lines of a text file?

For a project that I am doing, one of the things that I must do is delete the first X lines of a plaintext file. I'm saying X because I will need to do this routine multiple times and each time, the lines to delete will be different, but they will always start from the beginning, delete the first X and then output the results to the same file.

I am thinking about doing something like this, which I pieced together from other tutorials and examples that I read:

String line = null;

String tempFile = Path.GetTempFileName();
String filePath = openFileDialog.FileName;

int line_number = 0;
int lines_to_delete = 25;

using (StreamReader reader = new StreamReader(originalFile)) {
    using (StreamWriter writer = new StreamWriter(tempFile)) {
        while ((line = reader.ReadLine()) != null) {
            line_number++;

            if (line_number <= lines_to_delete)
                continue;

            writer.WriteLine(line);
        }
    }
}

if (File.Exists(tempFile)) {
    File.Delete(originalFile);
    File.Move(tempFile, originalFile);
}

But I don't know if this would work because of small stuff like line numbers starting at line 0 or whatnot... also, I don't know if it is good code in terms of efficiency and form.

Thanks a bunch.

like image 828
ankushg Avatar asked Dec 04 '22 14:12

ankushg


2 Answers

I like it short...

File.WriteAllLines(
    fileName,
    File.ReadAllLines(fileName).Skip(numberLinesToSkip).ToArray());
like image 175
Daniel Brückner Avatar answered Dec 06 '22 04:12

Daniel Brückner


It's OK, and doesn't look it would have the off-by-one problems you fear. However a leaner approach would be afforded by two separate loops -- one to just count the first X lines from the input file (and do nothing else), a separate one to just copy the other lines from input to output. I.e., instead of your single while loop, have...:

    while ((line = reader.ReadLine()) != null) {
        line_number++;

        if (line_number > lines_to_delete)
            break;
    }

    while ((line = reader.ReadLine()) != null) {
        writer.WriteLine(line);
    }
like image 43
Alex Martelli Avatar answered Dec 06 '22 03:12

Alex Martelli