How to remove empty/blank lines from a file in Unix (including spaces)?

426,304

Solution 1

This sed line should do the trick:

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

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

Solution 2

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:

Solution 3

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.

Solution 4

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

Like this

grep -Ev "^$" file.txt

Solution 5

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.

Share:
426,304

Related videos on Youtube

Khan
Author by

Khan

Updated on September 18, 2022

Comments

  • Khan
    Khan over 1 year

    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
    
  • MShoubaki
    MShoubaki about 13 years
    It actually needs to be "/^ *$/d" to remove lines that only contain spaces.
  • kasperd
    kasperd about 8 years
    @SeanReifschneider That requirement was not in the question when this answer was written?
  • mrswadge
    mrswadge over 6 years
    @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.
  • Trevor Boyd Smith
    Trevor Boyd Smith about 6 years
  • Leo
    Leo about 6 years
    grep . seems to be the simplest solution.
  • Govind Kailas
    Govind Kailas about 5 years
    If you want to skip the commented and blank lines, especially while dealing with conf files use grep -Ev '^#|^$' file.txt
  • ishandutta2007
    ishandutta2007 about 5 years
    I get bad flag in substitute command: 'e'
  • wisbucky
    wisbucky about 5 years
    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 ^$
  • kenorb
    kenorb about 5 years
    @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.
  • wisbucky
    wisbucky about 5 years
    @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)
  • Jim L.
    Jim L. almost 5 years
    grep . will match lines containing only spaces, which the OP says is not desired.
  • kenorb
    kenorb almost 5 years
    @JimL. A dot (.) in grep matches any character, so it'll print all non-empty lines. Even the lines with a single empty space are going to be printed.
  • Jim L.
    Jim L. almost 5 years
    Yes, and the OP wishes lines containing only spaces to be removed.
  • kenorb
    kenorb almost 5 years
    @JimL. I've improved answer with the solution removing lines with spaces.
  • kenorb
    kenorb almost 5 years
    This command is not compatible with macOS/BSD, secondly it does not produce the same output as OP requested (it produces 5 lines, not 4).
  • kenorb
    kenorb almost 5 years
    This command does not produce the same output as OP requested (it produces 5 lines, not 4).
  • kenorb
    kenorb almost 5 years
    Actually, I think none of the other answers providing what OP requested (exempt Steven one).
  • kenorb
    kenorb almost 5 years
    This produces 6 lines, not 4 as OP requested.
  • Yet Another User
    Yet Another User almost 4 years
    grep (GNU grep) 3.4 requires -E if you are using @GovindKailas' command