bash: read line and keep spaces

Viewed 4815

I am trying to read lines from a file containing multiple lines. I want to identify lines that contain only spaces. By definition, an empty line is empty and does not contain anything (including spaces). I want to detect lines that seems to be empty but they are not (lines that contain spaces only)

    while read line; do
        if [[ `echo "$line" | wc -w` == 0 && `echo "$line" | wc -c` > 1 ]];
        then
             echo "Fake empty line detected"
        fi
    done < "$1"

But because read ignores spaces in the start and in the end of a string my code isn't working.

an example of a file

hi
 hi
(empty line, no spaces or any other char)
hi
  (two spaces)
hey

Please help me to fix the code

2 Answers
Related