user2014290
user2014290

Reputation: 11

how to search for blank lines in bash

I am trying to create an if statement that performs an action when it reads a blank line. I would assume it would be something like this : if ($line=='\n');then where line is the line that it is reading from a text file. But this is not working.

Upvotes: 1

Views: 119

Answers (3)

michaelmeyer
michaelmeyer

Reputation: 8205

Or also:

grep -q '.' <<< $line

Returns 1 if line is empty, 0 if non-empty

Upvotes: 0

Kent
Kent

Reputation: 195029

try this:

if [[ "x$line" == "x" ]]; then...

or

if [[ "$line" =~ "^$" ]]; ...

Upvotes: 1

EJK
EJK

Reputation: 12527

while read line; do
    if [ "$line" = "" ]; then
         echo BLANK
    fi
done < filename.txt

or a slight variation:

while read line; do
    if [ "$line" ]; then
         echo NOT BLANK
    else
         echo BLANK
    fi
done < filename.txt

Upvotes: 1

Related Questions