I am trying to cleanup some files I get on a quarterly basis. They have a bunch of repeating headers and I would like to replace multiple string values at a single time. I can remove one string at a time, but I am not understanding how I can stream the file and look at each line and remove if it is String 1 or String 2.
Each file has at least 100-300 thousand lines and I get between 10 and 50 files each time the data is dumped to me about once a quarter... Would be easier if they didn't add these lines, but that is not an option.
Sorry for the newbie question, but I don't get to code very often. Any help is appreciated...
static void Main(string[] args)
{
string tempFile = Path.GetTempFileName();
string t1 = "-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------";
string fName = "C:\\PoC\\test\\test.txt";
using (var sr = new StreamReader(fName))
using (var sw = new StreamWriter(tempFile))
{
string line;
while ((line = sr.ReadLine()) != null)
{
if (line.Contains(t1) == false)
{
sw.WriteLine(line);
}
}
sr.Close();
sw.Close();
}
File.Delete(fName);
File.Move(tempFile, fName);
}