7

I don't have a code example here since I'm not sure how to do this at all, but I have a file. A legal empty line is one that only contains the new-line tab. Spaces or tabs are illegal.

How do I check if a line is "legally empty"?

If it doesn't have any words (I can check this with wc -w), how do I check if it has no spaces or tabs either, just new-line?

So I've tried something like this:

while read line; do
    if [[ "$line" =~ ^$ ]]; then
        echo empty line
        continue
    fi
done < $1

But it's not working. If I put a " " in an otherwise empty line, it still considers it empty.

3 Answers 3

8

If you want the line numbers of those empty lines:

perl -lne 'print $. if(/^$/)' your_file

If you want to delete those lines without Perl:

grep . your_file >new_file

If you want to delete those empty line in place using Perl:

perl -i -lne 'print if(/./)' your_file
1
  • the goal is to return an error if an empty line is not really empty (meaning contains spaces or tabs). I don't want to delete them and I can't use perl Dec 26, 2013 at 17:40
6

Terminology: a line that contains only white space is a blank line. A line that contains nothing (except for the newline terminator) is an empty line.

The read builtin strips off leading and trailing whitespace. So if it encounters a blank line, it sets its argument to an empty string, regardless of the amount of whitespace. To avoid this behavior and return the input line unmodified, set the field separator characters to nothing (by default, they are space, tab and newline): set the IFS variable to the empty string. See Why is while IFS= read used so often, instead of IFS=; while read..? for a more detailed explanation. While you're at it, pass the -r option to read, unless you want backslash-newline sequences to be a line continuation.

while IFS= read -r line; do
  if [ -z "$line" ]; then
    echo empty line
  fi
done <"$1"

If you want to tell whether a line is blank:

while IFS= read -r line; do
  case "$line" in
    '') echo "empty line";;
    *[![:space:]]*) echo "non-blank line";;
    *) echo "non-empty blank line";;
  esac
done <"$1"

You can use Bash regular expression matching if you prefer:

while IFS= read -r line; do
  if [[ "$line" =~ ^$ ]]; then
     echo "empty line"
  elif [[ "$line" =~ ^[[:space:]]+$ ]]; then
    echo "non-empty blank line"
  else
    echo "non-blank line"
  fi
done <"$1"

These can be done with pattern matching too (using shell wildcards, which have a different syntax from common regular expressions):

while IFS= read -r line; do
  if [[ "$line" == "" ]]; then
     echo "empty line"
  elif [[ "$line" != *[![:space:]]* ]]; then
    echo "non-empty blank line"
  else
    echo "non-blank line"
  fi
done <"$1"

If you merely want to look for empty lines in the file and aren't processing the lines in any other way, you can use grep:

if grep -qxF '' <"$1"; then
  echo "$1 contains an empty line"
fi

If you're looking for blank lines that are not empty:

if grep -Ex '[[:space:]]+' <"$1"; then
  echo "$1 contains a non-empty blank line"
fi
1
  • @BinaryZebra You do need to quote on the right of the equal sign if you have a variable containing a string rather than a pattern. In an question that isn't for advanced users, I prefer to keep things simple and not have to explain these subtleties. I won't show -z in this section since its point is to show ==, I showed -z just above. Jul 24, 2015 at 7:31
4

You can check for an empty line with the

^$

^ is the beginning of a line, $ is the end of a line, the above matches if there are no other characters.

You can now use that in e.g.

sed '/^$/d' input.txt

This would delete all empty lines from your input file. This would remove empty lines from the file and display the file content on console. The file still remains unchanged.

If you want to remove the empty lines from the file (meaning, changing the file content), then run:

sed -i '/^$/d' input.txt
2
  • 1
    so it would look something like this: grep -c ^$ $1... and if it prints 0 than means the line is empty? Dec 26, 2013 at 13:22
  • Yes, if you want to count the empty lines.
    – pfnuesel
    Dec 26, 2013 at 14:28

Your Answer

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

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