5

I am trying to delete all the blank lines in a text file. Is there a quick way to do this?

What I have

line one

line two

What I want

line one
line two

I am using Notepad++, so a solution using Notepad++ is appreciated.

I have Cygwin too, so if nothing else, a script is fine.

4 Answers 4

18

Use the menu:

1) Select all text (Ctrl-A)

2) TextFX -> TextFX Edit -> Delete Blank Lines

0
2

Macro's can be scary, accidental loss the first chars if its off a line..

  • View > Show Symbols > Show End Of Line

Take note that it ends in CR LF (This is Carriage Return and Line Feed which is \r\n) Ctrl+H then enable extended replace "\r\n\r\n" with "\r\n"

It will cycle through and remove the double spacing, you might need to repeat if its more than just double spacing. (Another thing to consider is: Edit > Trim Trailing White Space.)

2

Switch to Extended search mode in the Replace dialog.

Find what: \r\n\r\n

Replace with: \r\n

Press Replace All. All the blank lines are gone.

2
  • 1
    This does strongly depend on the line ending format used in the file. Commented Sep 3, 2009 at 21:24
  • 2
    or \n\n to \n for *nix
    – pelms
    Commented Sep 3, 2009 at 21:51
1

In Cygwin:

$ sed -i '/^$/d' <name_of_your_textfile.txt>

Or, if the "blank lines" are allowed to contain tabs and spaces:

$ sed -i '/^[ \t]*$/d' <name_of_your_textfile.txt>

The -i stands for "edit in place", so if your text files are valuable you might want to use the commands without the -i and pipe the output to a different file, look at the results and then rename it.

Using Vim (should come with Cygwin, but there is also a native Windows version):

:%g/^[ \t]*$/d

This has the advantage that you directly see your results and can undo them by pressing "u". You also might encounter fewer problems with Windows' line endings (depends on your Cygwin configuration).

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .