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 Np++ is appreciated.

Edit: I have cygwin too, so if nothing else, a script is fine.

link|improve this question

60% accept rate
feedback

4 Answers

Use the menu:

1) Select all text (Ctrl-A)

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

link|improve this answer
1  
Thanks, Efficient method. – greyDrifter Sep 3 '09 at 21:22
feedback

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.)

link|improve this answer
feedback

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.

link|improve this answer
This does strongly depend on the line ending format used in the file. – Manuel Faux Sep 3 '09 at 21:24
2  
or \n\n to \n for *nix – pelms Sep 3 '09 at 21:51
feedback

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 you textfiles 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 less problems with windows line endings (depends on your cygwin configuration).

link|improve this answer
feedback

protected by Diago Nov 9 '10 at 6:43

This question is protected to prevent "thanks!", "me too!", or spam answers by new users. To answer it, you must have earned at least 10 reputation on this site.

Not the answer you're looking for? Browse other questions tagged or ask your own question.