node ninja
node ninja

Reputation: 33056

Remove blank lines with grep

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

Upvotes: 258

Views: 391420

Answers (18)

Andrew
Andrew

Reputation: 1457

You can either:

(1) show only non-empty lines:

grep .

or

grep '\S'

or

grep '[[:graph:]]'

(2) hide empty lines:

grep -v '^$'

or better:

grep -v '^\s*$'

or

grep -v '^[[:space:]]*$'
  • . or '.' = pattern matching any character . except line break (typically patterns should be quoted when grep is used in a shell command, but in this case we can skip them)
  • '\S' = pattern, where \S stands for a character that is not a white space character (read more on pcre2syntax - Perl-compatible regular expressions)
  • '[[:graph:]]' = pattern, where [[:xxxxx:]] stands for positive POSIX named set, and graph for pcre2syntax's character class "printing, excluding space"
  • -v = invert the sense of matching, to select non-matching lines (read more on grep's Matching Control options)
  • '^$' = pattern, where ^ stands for start of line, and $ for end of line (which equals to empty lines) - this will work with LF end of line sequence (Unix and Unix-like systems)
  • \s* = \s is a whitespace character and * means zero or more occurrences, so pattern '^\s*$' will work with both LF (Unix) and CRLF (Windows) end of line sequence
  • '^[[:space:]]*$' = pattern, where ^ stands for start of line, [[:xxxxx:]] for POSIX named set, space for PCRE2 character class "white space", * for zero or more occurrences and $ for end of line - this will also work with LF and CRLF files

On some versions of grep you may also have to use this option:

  • -E = extended-regexp - interpret pattern as extended regular expressions

Upvotes: 2

ADV-IT
ADV-IT

Reputation: 851

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

Upvotes: 0

ars
ars

Reputation: 123588

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

Upvotes: 413

Federico Alemany
Federico Alemany

Reputation: 47

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

 grep -v "^#" file.txt | grep -v ^[[:space:]]*$

Upvotes: 3

jspek
jspek

Reputation: 446

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

Upvotes: 5

MarcT
MarcT

Reputation: 33

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

Upvotes: 2

FatherMathew
FatherMathew

Reputation: 990

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.

Upvotes: 4

lauc.exon.nod
lauc.exon.nod

Reputation: 113

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

Upvotes: 0

baitisj
baitisj

Reputation: 239

Use:

grep pattern filename.txt | uniq

Upvotes: 0

ghostdog74
ghostdog74

Reputation: 343143

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"

Upvotes: 39

mvds
mvds

Reputation: 47134

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

printf "\r" | egrep -xv "[[:space:]]*"

Upvotes: 1

Jonni2016aa
Jonni2016aa

Reputation: 154

egrep -v "^\s\s+"

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

The + duplicates current pattern.

The ^ is for the start

Upvotes: 0

Senol Erdogan
Senol Erdogan

Reputation: 61

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.

Upvotes: 6

Frej Connolly
Frej Connolly

Reputation: 1444

Keep it simple.

grep . filename.txt

Upvotes: 126

Sepero
Sepero

Reputation: 4677

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

Upvotes: 20

Majid Azimi
Majid Azimi

Reputation: 5745

Using Perl:

perl -ne 'print if /\S/'

\S means match non-blank characters.

Upvotes: 0

Tim
Tim

Reputation: 21

awk 'NF' file-with-blank-lines > file-with-no-blank-lines

Upvotes: 2

chryss
chryss

Reputation: 7519

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

Upvotes: 3

Related Questions