Delete blank lines from a text file

12,188

Solution 1

Use the menu:

1) Select all text (Ctrl-A)

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

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

Solution 3

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.

Solution 4

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

Share:
12,188

Related videos on Youtube

Kapsh
Author by

Kapsh

Updated on September 17, 2022

Comments

  • Kapsh
    Kapsh almost 2 years

    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.

  • Manuel Faux
    Manuel Faux almost 15 years
    This does strongly depend on the line ending format used in the file.
  • pelms
    pelms almost 15 years
    or \n\n to \n for *nix