233

I tried grep -v '^$' in Linux and that didn't work. This file came from a Windows file system.

17 Answers 17

394

Try the following:

grep -v -e '^$' foo.txt

The -e option allows regex patterns for matching.

The single quotes around ^$ makes it work for Cshell. Other shells will be happy with either single or double quotes.

UPDATE: This works for me for a file with blank lines or "all white space" (such as windows lines with \r\n style line endings), whereas the above only removes files with blank lines and unix style line endings:

grep -v -e '^[[:space:]]*$' foo.txt
5
  • That egrep would only work for files with zero or 1 space on the line, not for files with 2 or more spaces. Change ? to *.
    – Ed Morton
    Dec 8, 2012 at 9:42
  • 8
    This should be grep -E -v, everything after -e is interpreted as the pattern.
    – jazzpi
    Aug 3, 2015 at 17:10
  • 15
    grep -v -e '^[[:space:]]*$' -e '^#' file will give you all non-blank, non-comment lines in a script or config file (or any file type that uses the hash character for comments).
    – palswim
    Feb 27, 2018 at 23:11
  • 6
    "The -e option allows regex patterns for matching." That is very misleading. -e is a (POSIX-)definition for: This can be used to specify multiple search patterns, or to protect a pattern beginning with a hyphen (-). (from the manual). Grep already expects a (basic) regular expression by default. For this pattern, you may leave out -e entirely: grep -v '^[[:space:]]*$' foo.txt.
    – Yeti
    Jul 1, 2020 at 10:22
  • If you're dealing with files that might have windows-style CR+LF line breaks but don't want to also exclude lines with other whitespace, then use this regex: '^[[:cnrl:]]?$'.
    – Chris
    Feb 3, 2021 at 21:20
115

Keep it simple.

grep . filename.txt
8
  • 3
    this gives me all the lines in the file
    – phuclv
    Jan 24, 2017 at 6:11
  • 3
    @LưuVĩnhPhúc It should output all the lines in the file except blank lines. Feb 19, 2017 at 23:33
  • 4
    This works for me on files from a linux based system but not on files from Windows. Presumably because of Windows line-ending characters.
    – user5012123
    Feb 9, 2018 at 15:07
  • 1
    I'm upvoting this even though it doesn't quite solve the OP's problem of handling a file with Windows line endings, but since I don't have that issue, this turned out to be the perfect solution for me.
    – David Z
    Apr 16, 2018 at 1:27
  • 3
    This is the perfect solution. Simple and worked on Linux.
    – W00f
    Feb 21, 2020 at 9:45
39

Use:

$ dos2unix file
$ grep -v "^$" file

Or just simply awk:

awk 'NF' file

If you don't have dos2unix, then you can use tools like tr:

tr -d '\r' < "$file" > t ; mv t "$file"
3
  • 1
    Can't find the program dos2unix. Is that for Windows? the ask command doesn't work either.
    – node ninja
    Aug 8, 2010 at 1:36
  • ask? No, that's awk.
    – iconoclast
    Oct 3, 2014 at 18:19
  • Good point about converting to UNIX-style line endings otherwise regular expressions may not work as expected. Nothing here worked for me until I converted the line endings.
    – Ryan H.
    Aug 31, 2016 at 14:41
19
grep -v "^[[:space:]]*$"

The -v makes it print lines that do not completely match

===Each part explained===
^             match start of line
[[:space:]]   match whitespace- spaces, tabs, carriage returns, etc.
*             previous match (whitespace) may exist from 0 to infinite times
$             match end of line

Running the code-

$ echo "
> hello
>       
> ok" |
> grep -v "^[[:space:]]*$"
hello
ok

To understand more about how/why this works, I recommend reading up on regular expressions. http://www.regular-expressions.info/tutorial.html

2
  • 2
    How and why does this work? Your answer would be much better if you could explain. For instance your regular expression matches the beginning of the string then one or more spaces using the POSIX standard then the end of the string, i.e. with grep -v it removes all lines that are only spaces. Right? What happens if there are no spaces; it's simply a newline character?
    – Ben
    Dec 7, 2012 at 21:20
  • As my example shows, even only an empty line is removed (the first line). I added more information, so hopefully that helps. :)
    – Sepero
    Dec 8, 2012 at 1:02
6

If you have sequences of multiple blank lines in a row, and would like only one blank line per sequence, try

grep -v "unwantedThing" foo.txt | cat -s

cat -s suppresses repeated empty output lines.

Your output would go from

match1



match2

to

match1

match2

The three blank lines in the original output would be compressed or "squeezed" into one blank line.

4

The same as the previous answers:

grep -v -e '^$' foo.txt

Here, grep -e means the extended version of grep. '^$' means that there isn't any character between ^(Start of line) and $(end of line). '^' and '$' are regex characters.

So the command grep -v will print all the lines that do not match this pattern (No characters between ^ and $).

This way, empty blank lines are eliminated.

1
  • -e does not mean "the extended version of grep", maybe you are confused with -E? The manual clearly says that -e just explicitly says that a pattern follows. Since the pattern does not start with a dash, and you are only defining one pattern anyway, you might as well leave it out as by default grep expects one regex pattern: grep -v '^$' foo.txt (no need for extended regex functionality). Also it is worth mentioning that this does not eliminate the blank lines in the file, only that which is piped through the output. For that case, sed -i would be the right tool.
    – Yeti
    Jul 1, 2020 at 10:09
4

Do lines in the file have whitespace characters?

If so then

grep "\S" file.txt

Otherwise

grep . file.txt

Answer obtained from: https://serverfault.com/a/688789

3

I prefer using egrep, though in my test with a genuine file with blank line your approach worked fine (though without quotation marks in my test). This worked too:

egrep -v "^(\r?\n)?$" filename.txt
1
  • Tried that. Blank lines are still showing. Could this be because the file was made in Windows?
    – node ninja
    Aug 8, 2010 at 0:25
3

This code removes blank lines and lines that start with "#"

 grep -v "^#" file.txt | grep -v ^[[:space:]]*$
1
  • why not just a simple: grep -vE '^$|#' file.txt
    – youssef
    Sep 3, 2022 at 20:37
2
awk 'NF' file-with-blank-lines > file-with-no-blank-lines
2

It's true that the use of grep -v -e '^$' can work, however it does not remove blank lines that have 1 or more spaces in them. I found the easiest and simplest answer for removing blank lines is the use of awk. The following is a modified a bit from the awk guys above:

awk 'NF' foo.txt

But since this question is for using grep I'm going to answer the following:

grep -v '^ *$' foo.txt

Note: the blank space between the ^ and *.

Or you can use the \s to represent blank space like this:

grep -v '^\s*$' foo.txt
1

I tried hard, but this seems to work (assuming \r is biting you here):

printf "\r" | egrep -xv "[[:space:]]*"
1
  • That works if I replace the first part with output from the file.
    – node ninja
    Aug 8, 2010 at 1:38
0

Using Perl:

perl -ne 'print if /\S/'

\S means match non-blank characters.

0

egrep -v "^\s\s+"

egrep already do regex, and the \s is white space.

The + duplicates current pattern.

The ^ is for the start

0

Use:

grep pattern filename.txt | uniq
2
  • uniq will reduce adjoining blank lines to just one blank line, but does not remove them completely. Still, I like trying to use uniq like that. Sorting first would effectively remove all blank lines--leaving just one, but rearranging the line order may not be acceptable.
    – Zach Young
    Jan 16, 2013 at 2:43
  • Good point. This will also chomp repeated lines. I guess my solution introduces bugs.
    – baitisj
    Jan 17, 2013 at 5:57
0

Here is another way of removing the white lines and lines starting with the # sign. I think this is quite useful to read configuration files.

[root@localhost ~]# cat /etc/sudoers | egrep -v '^(#|$)'
Defaults    requiretty
Defaults   !visiblepw
Defaults    always_set_home
Defaults    env_reset
Defaults    env_keep =  "COLORS DISPLAY HOSTNAME HISTSIZE INPUTRC KDEDIR
LS_COLORS"
root    ALL=(ALL)       ALL
%wheel  ALL=(ALL)       ALL
stack ALL=(ALL) NOPASSWD: ALL
0

Read lines from file exclude EMPTY Lines

grep -v '^$' folderlist.txt

folderlist.txt

folder1/test

folder2
folder3

folder4/backup
folder5/backup

Results will be:

folder1/test
folder2
folder3
folder4/backup
folder5/backup

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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