How to remove double newline in text files


Last updated on July 5th, 2015 at 04:03 pm

As part of the hosting migration, I had some text files that had an extra empty line in them. This was caused by a newline followed by another newline.

On my pc I can search and replace for these using a good text editor. But it?s a bit harder to do on a linux command line.

The fix if it is just an empty new line is to use sed

sed '/^$/d'

as in

sed '/^$/d' < textfile.txt > newtextfile.txt

If the new lines have whitespace (a space or a tab) then use

sed '/^[ \t]*$/d'

You can also get information about the file with the file command

file textfile.txt
textfile.txt: ASCII English text

Beware if the output is

textfile.txt: ISO-8859 text, with CRLF, LF line terminators

as you?ll need to use

dos2unix textfile.txt

to convert it to a unix text file.

For people searching for the cause, most of these double new lines occured because a PHP script included another PHP script with require_once. The included PHP file had a whitespace at the end of the file after the

?>

close.


Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.