3

I have file which does not have any data in it Need to check below scenario and return file is empty otherwise not empty

if file contains no data but as only spaces return it as FILE is EMPTY 
if file contains no data but as only tabs   return it as FILE is EMPTY
if file contains no data but as only empty new line  return it as FILE is EMPTY  

Does this below code will satisfy all my above cases ? or any best approach all in one go

if [ -s /d/dem.txt ]
then
    echo "FILE IS NOT EMPTY AS SOME DATA"
else
    echo "FILE IS EMPTY NOT DATA AVAILABLE"
fi 
3
  • 2
    What should happen if file has spaces and tabs?
    – Daweo
    Commented Jun 9, 2021 at 10:43
  • @codeholic24 : -s tests the file size and hence is agnostic to white space inside the file. You can not make a decision on the content of a file, unless you look into the file. Commented Jun 9, 2021 at 12:14
  • @Daweo Basically i need to count number of records in file .. if file does not have any records but only space or empty line or tabs then it will count it as record that should not happen Commented Jun 9, 2021 at 12:36

3 Answers 3

5

You may use this awk for this:

awk 'NF {exit 1}' file && echo "empty" || echo "not empty"

Condition NF will be true only if there is non-whitespace character in the file.

7
  • 1
    can you explain in detail Commented Jun 9, 2021 at 10:51
  • @codeholic24: I have this explanation in answer Condition NF will be true only if there is non-whitespace character in the file. NF represents number of fields in a line and condition NF will be true only if there are non-space characters in a line.
    – anubhava
    Commented Jun 9, 2021 at 10:53
  • 4
    In all awk: awk 'NF{exit r=1}END{print (r?"not ":"") "empty"}' Commented Jun 9, 2021 at 10:58
  • @anubhava i am checking will confirm you Commented Jun 9, 2021 at 11:24
  • @anubhava is their any way to store the status of awk command if fail return 1 or pass return 0 , i can see that the above command exit the code that i don't want , their are few more lines in my script which i want to execute on basis of the above status Commented Jun 9, 2021 at 11:31
5

Your description is a bit unclear (what do you want to do with a file that contains spaces, tabs, and newlines?), but it sounds like you just want to know if the file contains any non-whitespace characters. So:

 if grep -q '[^[:space:]]' "$file"; then
     printf "%s\n" "$file is not empty";
 else
     printf "%s\n" "$file contains only whitespace"
 fi
2
  • can you explain the code grep -q what it does Commented Jun 9, 2021 at 12:43
  • @codeholic24 grep -q pattern file checks to see if pattern exists in the file. -q suppresses all output. If the pattern is in the file, grep returns 0. If not, grep returns 1. Commented Jun 9, 2021 at 13:10
2

If you had run your code you would have realized that no, -s considers that files with spaces, tabs and/or new lines are not empty. I would do it like this:

myfile="some_file.txt"
T=$(sed -e 's/\s//g' "$i")
if [ -n "$T" ]; then
    echo "$i is NOT empty"
else
    echo "$i is empty"
fi
2
  • can you explain in detail Commented Jun 9, 2021 at 10:51
  • sed looks for any instance of a whitespace character (\s) and replaces it with nothing. Then it's stored in a variable (T), and bash checks that that variable is empty ([ -n "$T" ]). Commented Jun 9, 2021 at 11:00

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.