177

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 
3

11 Answers 11

210

This sed line should do the trick:

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

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

6
  • 32
    It actually needs to be "/^ *$/d" to remove lines that only contain spaces. Commented Mar 28, 2011 at 23:36
  • 4
    @SeanReifschneider That requirement was not in the question when this answer was written? Commented Apr 18, 2016 at 10:02
  • 9
    @SeanReifschneider Would "/^\s*$/d" not be better as it would include tabs? Whilst it's not mentioned in the original post, it seems a stronger option to me. Commented Sep 20, 2017 at 12:57
  • 3
    adding the \s* is the same solution given by stackoverflow question Commented Feb 21, 2018 at 19:17
  • I get bad flag in substitute command: 'e' Commented Apr 13, 2019 at 2:12
128

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:

9
  • 8
    grep . seems to be the simplest solution. Commented Mar 21, 2018 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 ^$ Commented Apr 25, 2019 at 23:09
  • 4
    @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. Commented Apr 25, 2019 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) Commented Apr 26, 2019 at 2:01
  • grep . will match lines containing only spaces, which the OP says is not desired. Commented Jul 19, 2019 at 21:53
39
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.

2
  • This command does not produce the same output as OP requested (it produces 5 lines, not 4). Commented Jul 22, 2019 at 9:31
  • 3
    You likely need sed '/^[[:space:]]*$/d' file.txt. Credits to stackoverflow.com/a/16414489/123897 Commented Sep 7, 2023 at 16:19
23

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

Like this

grep -Ev "^$" file.txt 
3
  • 6
    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. Commented Mar 28, 2011 at 22:45
  • If you want to skip the commented and blank lines, especially while dealing with conf files use grep -Ev '^#|^$' file.txt Commented Mar 7, 2019 at 4:11
  • 1
    grep (GNU grep) 3.4 requires -E if you are using @GovindKailas' command Commented Aug 15, 2020 at 4:07
22

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.

10

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

cat file.txt | tr -s '\n' '\n' 
2
  • This produces 6 lines, not 4 as OP requested. Commented Jul 22, 2019 at 9:34
  • 1
    the second '\n' seems to be unnecessary Commented Mar 16, 2023 at 22:17
2

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 
1

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 
1

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

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

0

The simplest solution I have ever found:

cat file.txt | strings 
1
  • It's not optimal. The strings command is designed to extract printable text from binary files, not for text processing. While it does remove non-printable characters, it doesn't specifically target blank lines. Does it remove blank lines? Yes, but not reliably. It removes lines containing only spaces or non-printable characters, but not in a predictable way. If your file contains special characters or non-ASCII text, strings may unintentionally filter them out. Cheers! Commented Feb 2 at 21:37
0

Remove lines that are blank or contain only spaces using sed:

sed '/^\ *$/d' file.txt 

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.