Delete specific line from a text file?

前端 未结 10 1609
既然无缘
既然无缘 2020-11-29 11:14

I need to delete an exact line from a text file but I cannot for the life of me workout how to go about doing this.

Any suggestions or examples would be greatly appr

相关标签:
10条回答
  • 2020-11-29 11:22

    What? Use file open, seek position then stream erase line using null.

    Gotch it? Simple,stream,no array that eat memory,fast.

    This work on vb.. Example search line culture=id where culture are namevalue and id are value and we want to change it to culture=en

    Fileopen(1, "text.ini")
    dim line as string
    dim currentpos as long
    while true
        line = lineinput(1)
        dim namevalue() as string = split(line, "=")
        if namevalue(0) = "line name value that i want to edit" then
            currentpos = seek(1)
            fileclose()
            dim fs as filestream("test.ini", filemode.open)
            dim sw as streamwriter(fs)
            fs.seek(currentpos, seekorigin.begin)
            sw.write(null)
            sw.write(namevalue + "=" + newvalue)
            sw.close()
            fs.close()
            exit while
        end if
        msgbox("org ternate jua bisa, no line found")
    end while
    

    that's all..use #d

    0 讨论(0)
  • 2020-11-29 11:32
    1. Read and remember each line

    2. Identify the one you want to get rid of

    3. Forget that one

    4. Write the rest back over the top of the file

    0 讨论(0)
  • 2020-11-29 11:33

    Are you on a Unix operating system?

    You can do this with the "sed" stream editor. Read the man page for "sed"

    0 讨论(0)
  • 2020-11-29 11:35

    If the line you want to delete is based on the content of the line:

    string line = null;
    string line_to_delete = "the line i want to delete";
    
    using (StreamReader reader = new StreamReader("C:\\input")) {
        using (StreamWriter writer = new StreamWriter("C:\\output")) {
            while ((line = reader.ReadLine()) != null) {
                if (String.Compare(line, line_to_delete) == 0)
                    continue;
    
                writer.WriteLine(line);
            }
        }
    }
    

    Or if it is based on line number:

    string line = null;
    int line_number = 0;
    int line_to_delete = 12;
    
    using (StreamReader reader = new StreamReader("C:\\input")) {
        using (StreamWriter writer = new StreamWriter("C:\\output")) {
            while ((line = reader.ReadLine()) != null) {
                line_number++;
    
                if (line_number == line_to_delete)
                    continue;
    
                writer.WriteLine(line);
            }
        }
    }
    
    0 讨论(0)
  • 2020-11-29 11:35

    You can actually use C# generics for this to make it real easy:

            var file = new List<string>(System.IO.File.ReadAllLines("C:\\path"));
            file.RemoveAt(12);
            File.WriteAllLines("C:\\path", file.ToArray());
    
    0 讨论(0)
  • 2020-11-29 11:37

    This can be done in three steps:

    // 1. Read the content of the file
    string[] readText = File.ReadAllLines(path);
    
    // 2. Empty the file
    File.WriteAllText(path, String.Empty);
    
    // 3. Fill up again, but without the deleted line
    using (StreamWriter writer = new StreamWriter(path))
    {
        foreach (string s in readText)
        {
            if (!s.Equals(lineToBeRemoved))
            {
                writer.WriteLine(s);
            }
        }
    }
    
    0 讨论(0)
提交回复
热议问题