How to remove empty/blank lines from a file in Unix (including spaces)?
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:
- How to remove blank lines from a file (including tab and spaces)?
- With
sed
: Delete empty lines using sed - With
awk
: Remove blank lines using awk
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.
Related videos on Youtube
Khan
Updated on September 18, 2022Comments
-
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
-
Admin about 9 yearsFor awk, see: Remove blank lines in awk, or using grep, in general, see: How to remove blank lines from a file in shell?
-
Admin about 6 years
-
Admin about 5 yearsThis is an epic answer that pretty much gives every possible solution using grep, sed, awk: stackoverflow.com/questions/16414410/…
-
-
MShoubaki about 13 yearsIt actually needs to be "/^ *$/d" to remove lines that only contain spaces.
-
kasperd about 8 years@SeanReifschneider That requirement was not in the question when this answer was written?
-
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 about 6 years
-
Leo about 6 years
grep .
seems to be the simplest solution. -
Govind Kailas about 5 yearsIf you want to skip the commented and blank lines, especially while dealing with conf files use
grep -Ev '^#|^$' file.txt
-
ishandutta2007 about 5 yearsI get
bad flag in substitute command: 'e'
-
wisbucky about 5 yearsThe 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. Compareunbuffer apt search foo | grep .
tounbuffer apt search foo | grep -v ^$
-
kenorb about 5 years@wisbucky You see colors, because
grep
is aliased togrep --color=auto
on your system (check by:type grep
). You can run it as\grep
or use--color=none
parameter. -
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. almost 5 years
grep .
will match lines containing only spaces, which the OP says is not desired. -
kenorb almost 5 years@JimL. A dot (
.
) ingrep
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. almost 5 yearsYes, and the OP wishes lines containing only spaces to be removed.
-
kenorb almost 5 years@JimL. I've improved answer with the solution removing lines with spaces.
-
kenorb almost 5 yearsThis 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 almost 5 yearsThis command does not produce the same output as OP requested (it produces 5 lines, not 4).
-
kenorb almost 5 yearsActually, I think none of the other answers providing what OP requested (exempt Steven one).
-
kenorb almost 5 yearsThis produces 6 lines, not 4 as OP requested.
-
Yet Another User almost 4 years
grep (GNU grep) 3.4
requires -E if you are using @GovindKailas' command