Home > Programming > Fixing Extra Newlines in Text Files

Fixing Extra Newlines in Text Files

September 11th, 2010

When editing source code you often come across files with additional blank lines that shouldn’t be there.

Extra blank lines

The problem is generally caused by someone editing the file with a text editor which doesn’t understand and maintain the different newline types. You then end up with a file containing a mix of different line endings.

Mixed line endings can have all sorts of unexpected consequences in your applications. Also, editing is really annoying since you have to do a lot more scrolling.

The common newline types are:

LF Unix, Linux and Mac OS X
CR Mac OS up to 9
CR+LF Windows

There’s more info in Wikipedia.

Removing the Additional Lines

The solution is to to use search and replace to get rid of the extra lines, and then convert all the line ending in the file to the same type.

First you need a text editor that can display the different types and also search for each of them separately. I recommend Notepad2 since it’s very good and also free.

If you open the file in Notepad2 then go to View –> “Show Line Endings” you’ll see the line endings:

Mixed line ending

Now that you know which line endings are in the file, you can bring up the Replace dialog:

Replace dialog
To search for the line endings, you need to tick “Transform backslahes”

Then search for the corresponding line end type:

\r CR
\n LF
\r\n CR+LF

In this example I’m going to search for all the CR+LF and replace them with nothing. Hit “Replace All” and your file is magically back to normal.

The only thing left to do is convert all the line endings to the correct type. Go to File –> “Line Endings” and chose whichever type you need (do this even if it’s already selected)

You have now got a file with consistent line endings and no extraneous blank lines:

Screenshot - 11_09_2010 , 18_03_38

Alternative Method

An alternative method is to convert the newlines first and then swap all the double newlines for singles.

After converting the line endings you get:

Screenshot - 11_09_2010 , 18_07_54

Next do the following Replace:

Screenshot - 11_09_2010 , 18_09_46

Now you should have the same result as above.

Categories: Programming Tags:
Comments are closed.