littlerunaway
littlerunaway

Reputation: 453

Checking for empty lines in a file

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.

Upvotes: 7

Views: 22895

Answers (3)

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

Upvotes: 7

Vijay
Vijay

Reputation: 67211

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

Upvotes: 8

pfnuesel
pfnuesel

Reputation: 15300

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

Upvotes: 4

Related Questions