how to scan for the empty or blank line

I am having a text file sample.txt

The contents of the sample.txt is as follows.
____________
aaa,bbb

ccc,ddd
____________

first line contains aaa,bbb
second line is blank
third line contains ccc,ddd

Now I need to write a script to search in the file sample.txt. If it contains blank as in second line. It have to throw the error message "invalid file" else nothing.

I have achieved this by reading line by line. But due to performance issue, I need to scan in the whole file whether it contains blank line. If so i need to throw the error message "invalid file".

Can anyone help me out.

Thanks in advance.

Krishnakanth Manivannan

Here is one solution, using awk:

awk '!NF{exit 1}' sample.txt || echo 'Invalid File'
 
grep -v . sample.txt && echo "Invalid File" || echo "Valid File"

---------- Post updated at 09:01 AM ---------- Previous update was at 08:59 AM ----------

And if your grep supprot -m option, then you can try the below

 
grep -m 1 -v . sample.txt && echo "Invalid File" || echo "Valid File"

so, whenever the first empty line (means no space also) was captured, it will not go further and scan your text file.

1 Like

Next solution is answer for question "Which files include empty lines or only spaces ?"

# line length 0 or line include only spaces
emptylines=$(  grep -l -e "^$" -e "^ *$"      *.txt   )
for f in $emptylines
do
        echo "Invalid File:$f"
done
1 Like
 
"^ .*"

The above pattern will match " abc" also

1 Like