Bypass a nawk snippet if the input file is empty
Instead of the NR == FNR
test to test whether you're processing the first file, you could do:
awk 'FILENAME == ARGV[1] {...} ...' file1 file2
But that's a more expensive test, so if file1
is a regular file, you might as well use @Archemar's approach and not run awk
at all if the first file is empty.
In cases (not yours) where file1
and file2
have to be the same file, you can do:
awk 'FILENAME == ARGV[1] {...} ...' file1 ./file1
Or:
awk 'FILENAME == "-" {...} ...' - <file1 file1
An even better approach (portable and efficient):
awk '!file1_processed {...} ...' file1 file1_processed=1 file2
If you need that to apply on ./*.txt
for instance, you'd do:
set -- ./*.txt
first=$1; shift
awk '!first_processed {...} ...' "$first" first_processed=1 "$@"
A GNU awk
-specific approach:
awk 'ARGIND == 1 {...} ...' file1 file2
there is a test
function for zero size file.
if test -s apple.txt
then
## apple.txt non empty
code ...
else
## file empty
fi