Sign up ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. It's 100% free, no registration required.

I have a document with a lot of empty lines.

How can I remove them when there are 2 or more together.

I tried sed "s/\n\n//" file but it didn't work. No error.

share|improve this question
Do I read you correctly if you do not want to remove all blank lines, but only if it is two or more. So not single blank lines? – Sukminder May 16 '13 at 13:31
And if it's two or more lines shall really all of them be deleted or just all but one? – Hauke Laging May 16 '13 at 13:44

7 Answers 7

up vote 19 down vote accepted

Just to remove empty lines:

sed  '/^$/d'

sed is line oriented, so thinking in terms of "2 or more of a particular byte" works except when that byte is a newline. Then you have to think of somethig that works for the whole line.

share|improve this answer
Of course! +1 for simple elegance. – terdon May 16 '13 at 13:22
sed is capable of handling several lines via its "pattern space" / "hold space" feature. But I feel that's too complicated. ;-) – Hauke Laging May 16 '13 at 13:42
This will not function as desired if the first character of the file is a newline. – Chris Down May 17 '13 at 3:42
@ChrisDown - are you certain? I tried it on just such a file, and it seemed to work with (GNU sed) 4.2.2. – Bruce Ediger May 17 '13 at 13:04
@BruceEdiger Note the first line -- – Chris Down May 17 '13 at 13:05

No need for sed. grep will do:

grep .

(that's grep, SPC, dot, that is match any line containing at least one character).

There's also:

tr -s '\n'

(squeeze any sequence of newline characters into one).

As noted by Chris, both are not equivalent because removing empty lines (like the first solution above and most other answers focus on here) is not the same as squeezing sequences of newline characters as requested in the case where the first line is empty as it only takes one leading newline character to make the first line empty.

share|improve this answer
This will not function as desired if the first character of the file is a newline: – Chris Down May 17 '13 at 3:42

sed is not the best tool for that, since it is line based and treats \n as the end-of-line character this gets complicated. Having seen @Bruce Ediger's answer sed may well be the perfect tool for the job, still, here are some other options:

  1. Perl

    perl -ne 'print if /./' file.txt


    perl -pe '$/=""; s/\n+/\n/;' file.txt 

    Thanks to @ruakh who made me go and read this:


    The input record separator, newline by default. This influences Perl's idea of what a "line" is. Works like awk's RS variable, including treating empty lines as a terminator if set to the null string (an empty line cannot contain any spaces or tabs). You may set it to a multi-character string to match a multi-character terminator, or to undef to read through the end of file. Setting it to "\n\n" means something slightly different than setting to "" , if the file contains consecutive empty lines. Setting to "" will treat two or more consecutive empty lines as a single empty line. Setting to "\n\n" will blindly assume that the next input character belongs to the next paragraph, even if it's a newline.

  2. gawk/awk

    awk '$1' file.txt

    That will work for the example posted but as @Stephane Chazelas pointed out, it will also delete lines whose first field "looks like" 0. This is more robust:

    awk NF file.txt
share|improve this answer
awk '$1' would remove the empty lines and the lines where the first field looks like 0. (like 0, 0e12, -0, 00, ` 0...). Use awk '$0 != ""'` or awk length (or awk NF if you want to remove blank lines). – Stéphane Chazelas May 16 '13 at 18:58
The first perl one would replace \n\n\n with `\n\n' – Stéphane Chazelas May 16 '13 at 19:03
For the first Perl one, you should actually set $/ to "" (which is a magic value for just this sort of purpose). – ruakh May 16 '13 at 22:40
@StephaneChazelas you're quite right on both counts, answer edited. – terdon May 17 '13 at 0:43
@ruakh thanks! I'd never heard of that one. – terdon May 17 '13 at 0:43

What do you mean remove? remove duplicate (many blank line to one) or remove all?

If you want to remove duplicate, here is the method using sed:

sed '$!N; /^\(.*\)\n\1$/!P; D'

It simulates uniq command.

The best choice is using awk:

awk NF <filename>
share|improve this answer

If you wanted to keep a single blank line for any given sequence of blank lines you might do:

sed -e '/./b' -e :n -e 'N;s/\n$//;tn'
share|improve this answer

Try sed -e 's#\\n\\n#\\n#g' input.file > output.file using / both as your field separator and part of your regex could be the problem.

share|improve this answer
Just gave this a whirl with one of my files containing double and triple newlines in a sequence. Doesn't work at all for me. – syntaxerror Nov 7 '13 at 19:53

For most of these answers it is first necessary to remove trailing whitespace. Removing doubled up newlines removes all blank lines. (Think about this).

Literally interpreted the OP wants "all blank lines removed from a file if there are any repeated blank lines".

The typical user wants to "remove only duplicated blank lines".

To do this, strip trailing whitepace first, and pipe though cat -s

sed  s/[[:space:]]*$// | cat -s

And yet this will not remove a superflous leading or trailing blank line.

share|improve this answer
Downvoted, but this clearly works ? No comment ? – mckenzm Feb 7 at 8:48

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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