60

How do I remove empty/blank (including spaces only) lines in a file in Unix/Linux using the command line?

contents of file.txt

Line:Text
1:<blank>
2:AAA
3:<blank>
4:BBB
5:<blank>
6:<space><space><space>CCC
7:<space><space>
8:DDD

output desired

1:AAA
2:BBB
3:<space><space><space>CCC
4:DDD

10 Answers 10

85

This sed line should do the trick:

sed -i '/^$/d' file.txt

The -i means it will edit the file in-place.

28

grep

Simple solution is by using grep (GNU or BSD) command as below.

  • Remove blank lines (not including lines with spaces).

    grep . file.txt
    
  • Remove completely blank lines (including lines with spaces).

    grep "\S" file.txt
    

Note: If you get unwanted colors, that means your grep is aliases to grep --color=auto (check by type grep). In that case, you can add --color=none parameter, or just run the command as \grep (which ignores the alias).


ripgrep

Similar with ripgrep (suitable for much larger files).

Remove blank lines not including lines with spaces:

rg -N . file.txt

or including lines with spaces:

rg -N "\S" file.txt

See also:

  • 2
    grep . seems to be the simplest solution. – Leo Mar 21 '18 at 21:36
  • The downside of grep . compared to the other solutions is that it will highlight all the text in red. The other solutions can preserve the original colors. Compare unbuffer apt search foo | grep . to unbuffer apt search foo | grep -v ^$ – wisbucky Apr 25 at 23:09
  • 1
    @wisbucky You see colors, because grep is aliased to grep --color=auto on your system (check by: type grep). You can run it as \grep or use --color=none parameter. – kenorb Apr 25 at 23:26
  • @kenorb If you use grep --color=none ., you will get all white text, which overrides the color formatting of the original command (example: apt search foo) – wisbucky Apr 26 at 2:01
  • grep . will match lines containing only spaces, which the OP says is not desired. – Jim L. Jul 19 at 21:53
27
sed '/^$/d' file.txt

d is the sed command to delete a line. ^$ is a regular expression matching only a blank line, a line start followed by a line end.

  • +1 for the explanation – Alex Raj Kaliamoorthy Feb 17 '18 at 10:26
  • This command does not produce the same output as OP requested (it produces 5 lines, not 4). – kenorb Jul 22 at 9:31
22

You can use the -v option with grep to remove the matching empty lines.

Like this

grep -Ev "^$" file.txt
  • 4
    I don't believe you need the -E, at least not with GNU grep, but apart from that I'm so pleased to see this done with grep! It's what I reach for in preference to sed, every time; in-line filters seem to me to be better than in-line editors. – MadHatter Mar 28 '11 at 22:45
  • If you want to skip the commented and blank lines, especially while dealing with conf files use grep -Ev '^#|^$' file.txt – Govind Kailas Mar 7 at 4:11
7

Here is an awk solution:

awk NF file.txt

With Awk, NF only set on non-blank lines. When this condition match, Awk default action is to print the whole line.

6

To remove empty lines, you could squeeze new line repeats with tr:

cat file.txt | tr -s '\n' '\n'
  • This produces 6 lines, not 4 as OP requested. – kenorb Jul 22 at 9:34
1

xargs if you dont mind stripping leading whitespace

$ docker run -it --rm alpine sh
/ # cat <<eof > /tmp/file
> one
>
>   two
> three
>
>
>   four
> eof
/ # cat /tmp/file
one

  two
three


  four
/ # cat /tmp/file | xargs -n1
one
two
three
four
0

Ex/Vim

Here is the method using ex editor (part of Vim):

ex -s +'v/\S/d' -cwq test.txt

For multiple files (edit in-place):

ex -s +'bufdo!v/\S/d' -cxa *.txt

Note: The :bufdo command is not POSIX.

Without modifying the file (just print on the standard output):

cat test.txt | ex -s +'v/\S/d' +%p +q! /dev/stdin
0

For me @martigin-heemels command was throwing error this fixed it (ie a dummy param to i),

sed -i '' '/^$/d' file.txt

0

Probably the easiest way to remove blank lines (without spaces) is to use cat -s:

$ cat -s file
$ some-command | cat -s

At least if you don't want to edit a file in-place but for example write to the terminal instead. Also, it doesn't involve any funny regex business so it's super easy to remember even for non-RE-friendly people.


From man cat:

-s, --squeeze-blank never more than one single blank line

Might be different on different OSes but was present on a few Linuxes and OpenBSD last time I checked.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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