How to find and replace text in a file

354,553

Solution 1

Read all file content. Make a replacement with String.Replace. Write content back to file.

string text = File.ReadAllText("test.txt");
text = text.Replace("some text", "new value");
File.WriteAllText("test.txt", text);

Solution 2

You're going to have a hard time writing to the same file you're reading from. One quick way is to simply do this:

File.WriteAllText("test.txt", File.ReadAllText("test.txt").Replace("some text","some other text"));

You can lay that out better with

string str = File.ReadAllText("test.txt");
str = str.Replace("some text","some other text");
File.WriteAllText("test.txt", str);

Solution 3

You need to write all the lines you read into the output file, even if you don't change them.

Something like:

using (var input = File.OpenText("input.txt"))
using (var output = new StreamWriter("output.txt")) {
  string line;
  while (null != (line = input.ReadLine())) {
     // optionally modify line.
     output.WriteLine(line);
  }
}

If you want to perform this operation in place then the easiest way is to use a temporary output file and at the end replace the input file with the output.

File.Delete("input.txt");
File.Move("output.txt", "input.txt");

(Trying to perform update operations in the middle of text file is rather hard to get right because always having the replacement the same length is hard given most encodings are variable width.)

EDIT: Rather than two file operations to replace the original file, better to use File.Replace("input.txt", "output.txt", null). (See MSDN.)

Solution 4

It is likely you will have to pull the text file into memory and then do the replacements. You will then have to overwrite the file using the method you clearly know about. So you would first:

// Read lines from source file.
string[] arr = File.ReadAllLines(file);

YOu can then loop through and replace the text in the array.

var writer = new StreamWriter(GetFileName(baseFolder, prefix, num));
for (int i = 0; i < arr.Length; i++)
{
    string line = arr[i];
    line.Replace("match", "new value");
    writer.WriteLine(line);
}

this method gives you some control on the manipulations you can do. Or, you can merely do the replace in one line

File.WriteAllText("test.txt", text.Replace("match", "new value"));

I hope this helps.

Solution 5

This is how I did it with a large (50 GB) file:

I tried 2 different ways: the first, reading the file into memory and using Regex Replace or String Replace. Then I appended the entire string to a temporary file.

The first method works well for a few Regex replacements, but Regex.Replace or String.Replace could cause out of memory error if you do many replaces in a large file.

The second is by reading the temp file line by line and manually building each line using StringBuilder and appending each processed line to the result file. This method was pretty fast.

static void ProcessLargeFile()
{
        if (File.Exists(outFileName)) File.Delete(outFileName);

        string text = File.ReadAllText(inputFileName, Encoding.UTF8);

        // EX 1 This opens entire file in memory and uses Replace and Regex Replace --> might cause out of memory error

        text = text.Replace("</text>", "");

        text = Regex.Replace(text, @"\<ref.*?\</ref\>", "");

        File.WriteAllText(outFileName, text);




        // EX 2 This reads file line by line 

        if (File.Exists(outFileName)) File.Delete(outFileName);

        using (var sw = new StreamWriter(outFileName))      
        using (var fs = File.OpenRead(inFileName))
        using (var sr = new StreamReader(fs, Encoding.UTF8)) //use UTF8 encoding or whatever encoding your file uses
        {
            string line, newLine;

            while ((line = sr.ReadLine()) != null)
            {
              //note: call your own replace function or use String.Replace here 
              newLine = Util.ReplaceDoubleBrackets(line);

              sw.WriteLine(newLine);
            }
        }
    }

    public static string ReplaceDoubleBrackets(string str)
    {
        //note: this replaces the first occurrence of a word delimited by [[ ]]

        //replace [[ with your own delimiter
        if (str.IndexOf("[[") < 0)
            return str;

        StringBuilder sb = new StringBuilder();

        //this part gets the string to replace, put this in a loop if more than one occurrence  per line.
        int posStart = str.IndexOf("[[");
        int posEnd = str.IndexOf("]]");
        int length = posEnd - posStart;


        // ... code to replace with newstr


        sb.Append(newstr);

        return sb.ToString();
    }
Share:
354,553

Related videos on Youtube

Win Coder
Author by

Win Coder

Updated on December 07, 2021

Comments

  • Win Coder
    Win Coder over 2 years

    My code so far

    StreamReader reading = File.OpenText("test.txt");
    string str;
    while ((str = reading.ReadLine())!=null)
    {
          if (str.Contains("some text"))
          {
              StreamWriter write = new StreamWriter("test.txt");
          }
    }
    

    I know how to find the text, but I have no idea on how to replace the text in the file with my own.

    • StackOrder
      StackOrder over 6 years
      Consider this comment as a tip only : If you have visual studio ,you can include the folders in the solution and use the search and replace feature of visual studio .Best of luck
    • RecklessSergio
      RecklessSergio almost 5 years
  • Alvin Wong
    Alvin Wong over 11 years
    This is simple but not desirable for very-large file. (p.s. I am not the one who downvoted)
  • Flynn1179
    Flynn1179 over 11 years
    I'd agree, but you can't write to the file while you're reading from it. Unless you write out to a different file, then replace it with a rename afterwards.. either way, the new file has to be stored somewhere else while you're building it, whether it's in memory or on disk.
  • Sergey Berezovskiy
    Sergey Berezovskiy over 11 years
    @WinCoder BTW for more complex replacements you can use Regex.Replace
  • Brent
    Brent over 9 years
    VB had to change 2 lines: Using input As New StreamReader(filename) While input.Peek() >= 0
  • Banshee
    Banshee almost 9 years
    This reads the entire file to memory at once, not always that good.
  • J.S. Orris
    J.S. Orris almost 9 years
    @Banshee Touche' I just tried to read 9,000,000 rows and was thrown a System out of memory exception.
  • Alexander
    Alexander over 8 years
    For large files it's more complex issue. Read byte chunk, analyze them, read another chunk, etc.
  • djv
    djv almost 8 years
    @Alexander Right. One chunk ends with "...som", and the next begins with "e text...". Makes it a much more complicated problem.
  • rollingthedice
    rollingthedice over 6 years
    Very helpful and simple solution!
  • SteveCinq
    SteveCinq almost 6 years
    @Flynn1179 Not true in this example. It works. Try it out. I guess the ReadAllText closes off file access prior to WriteAllText. I use this very technique in my own app.
  • Flynn1179
    Flynn1179 almost 6 years
    I know; this example doesn't write while it's reading, that was my point!
  • user1760150
    user1760150 over 5 years
    @djv For the chunking method, I think it might work if you move the file pointer back the length of the search text after each chunk. You'll have to make the same adjustment when writing the chunk to the new temp file, but i think that would cover the problem of the search text sometimes being in between chunks
  • user618520
    user618520 over 4 years
    How to realize it in VB? Thanks
  • user3548161
    user3548161 over 3 years
    It's a pretty good example i mean alot of people dont need to use this for large file processing .. some of us are happy with a tiny 2000 line file with a straight forward method like this.
  • Admin
    Admin about 3 years
    This also has another issue eg: when editing a line that has "1.2" to "1.234", it also affects another line if it contains"1.23"