How can you remove blank lines from a text file in C#?
Asked
Active
Viewed 4.9k times
15
-
Have to be a bit more specific. What kind of text file are you referring to? – Tieson T. Jun 25 '11 at 19:37
-
7@Dee Jay: -1 for -1ing spelling error, that's what the edit is for. – amit Jun 25 '11 at 19:38
-
5@Tieson T. Geez, can I be any more specific, simply remove blank lines from a text file, it has several lines, some which are blank, so which have strings on – Mike Jun 25 '11 at 19:39
-
@Dee Jay That's pretty harsh, I'm sure you make the occasional spelling mistake – Mike Jun 25 '11 at 19:39
-
6Mike, your question history indicates someone trying to program a simple application using only community help. That's selfish, and not really programming. Pick up a book, run through some tutorials - you'll be better off in the long-term. – Michael Petrotta Jun 25 '11 at 19:40
-
@Mile Yes, you can. C# has utilities that make working with XML easier, for instance, so it affects the answer I would give. – Tieson T. Jun 25 '11 at 19:43
-
@Mike: First of all, downvoting is free now, Secondly it showed that you don't put effort in your question. – user774411 Jun 25 '11 at 19:43
-
1@amit: Downvoting is free now, so DO blame the policy – user774411 Jun 25 '11 at 19:45
-
possible duplicate of [C# - Remove duplicate lines within a text file](http://stackoverflow.com/questions/6387675/c-remove-duplicate-lines-within-a-text-file) – Hans Passant Jun 25 '11 at 20:44
-
2@Hans Passant - this is definitely not a duplicate of the question that you refer to. This one asks how to remove empty lines, the one that you refer to asks how to remove duplicate lines. – Alex Aza Jun 25 '11 at 20:53
-
@Alex - trying to rescue a train wreck. Surely *you* see how there is no real difference between a duplicate and an empty line? Call an empty line a duplicate. – Hans Passant Jun 25 '11 at 20:58
-
2I think this is called 'Herd behavior'. Instead of focusing on the question, which is although simple, but quite clear, people starting nitpicking on spelling and other irrelevant things. All started just witn -1 on spelling, which is not even legit -1. People, why are you so angry? – Alex Aza Jun 25 '11 at 20:58
-
4@Hans Passant - the answers to those questions are different. Duplicate lines question is a little bit more complicated and the solution will be more memory consuming. Both questions have answers already, you can see that the answers are different and not-interchangeable. – Alex Aza Jun 25 '11 at 21:00
4 Answers
35
If file is small:
var lines = File.ReadAllLines(fileName).Where(arg => !string.IsNullOrWhiteSpace(arg));
File.WriteAllLines(fileName, lines);
If file is huge:
var tempFileName = Path.GetTempFileName();
try
{
using (var streamReader = new StreamReader(inptuFileName))
using (var streamWriter = new StreamWriter(tempFileName))
{
string line;
while ((line = streamReader.ReadLine()) != null)
{
if (!string.IsNullOrWhiteSpace(line))
streamWriter.WriteLine(line);
}
}
File.Copy(tempFileName, inptuFileName, true);
}
finally
{
File.Delete(tempFileName);
}

Alex Aza
- 76,499
- 26
- 155
- 134
-
2Not a good solution for huge files as the entire file could be 1 line and then you still read it all in memory with ReadLine()... – md1337 Apr 11 '12 at 20:14
-
1Interestingly when doing a search on this topic I found this blog post which backs this answer: http://chrisfulstow.com/counting-blank-lines-in-a-large-text-file-with-csharp-and-dotnet It basically shows metrics indicating for really large files use a `streamReader` otherwise just use the `ReadAllLines` method. – atconway May 29 '13 at 12:48
-
11
File.WriteAllLines(path, File.ReadAllLines(path).Where(l => !string.IsNullOrWhiteSpace(l)));

Mårten Wikström
- 11,074
- 5
- 47
- 87
4
Read all of the contents of a file into a string then just run
string output = null;
try {
output = Regex.Replace(input, @"^\s*$", "", RegexOptions.Multiline);
} catch (Exception e) {
}
Other similar options can be found in How to remove empty lines from a formatted string?

Community
- 1
- 1

Daniel Iankov
- 306
- 3
- 7
1
We can achieve it very easily by using LINQ technique for Huge or small file. 1.Explanation: It will read the file and skip all empty lines and store all the data into an string array
string[] text = File.ReadAllLines(path with file name).Where(s => s.Trim() != string.Empty).ToArray();
It will delete that file.
File.Delete(path with file name);
It will create new file as same name and append all the array data into new file
File.WriteAllLines(path with file name, text);
Complete Code
string[] text = File.ReadAllLines(LoraWan_Parameter_Check_Tool.Properties.Settings.Default.csv_file_path.ToString()).Where(s => s.Trim() != string.Empty).ToArray(); File.Delete(LoraWan_Parameter_Check_Tool.Properties.Settings.Default.csv_file_path.ToString()); File.WriteAllLines(LoraWan_Parameter_Check_Tool.Properties.Settings.Default.csv_file_path.ToString(), text);
- Problem solved
Thank you

Dilip Kumar Choudhary
- 429
- 5
- 5