Reputation: 11
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
Reputation: 8205
Or also:
grep -q '.' <<< $line
Returns 1 if line
is empty, 0 if non-empty
Upvotes: 0
Reputation: 195029
try this:
if [[ "x$line" == "x" ]]; then...
or
if [[ "$line" =~ "^$" ]]; ...
Upvotes: 1
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