Reputation: 2140
I am trying to check if a file is empty using awk
in a bash script. This condition for a file being empty is the following: If a file only has its first row with field values followed by ------
(and followed by nothing else) then it is considered empty.
Here is the structure of that special file:
file.txt
col1 col2 col3 col4 col5
------
So far this is what I have:
awk '
{
if (NR > 2)
print "NOT EMPTY"
else
print "EMPTY"
}
' file.txt
The output I want is:
EMPTY
The empty I am getting is:
EMPTY
EMPTY
How do I get only EMPTY
as one output? I know it might be very simple but I couldn't find anything. Thanks!
Let me know if further explanation is needed.
EDIT: Also, if extra blank lines are added to file.txt, then it will print EMPTY
for as many extra line there is. Is there a more general approach to this?
Upvotes: 4
Views: 5736
Reputation: 786091
You need to put your awk logic in END
block otherwise it is executed for each line of input:
awk '
END {
if (NR > 2)
print "NOT EMPTY"
else
print "EMPTY"
}
' file.txt
Upvotes: 6