manpagez: man pages & more
info gawk
Home | html | info | man

File: gawk.info,  Node: Empty Files,  Next: Ignoring Assigns,  Prev: File Checking,  Up: Data File Management

10.3.4 Checking for Zero-Length Files
-------------------------------------

All known 'awk' implementations silently skip over zero-length files.
This is a by-product of 'awk''s implicit
read-a-record-and-match-against-the-rules loop: when 'awk' tries to read
a record from an empty file, it immediately receives an end-of-file
indication, closes the file, and proceeds on to the next command-line
data file, _without_ executing any user-level 'awk' program code.

   Using 'gawk''s 'ARGIND' variable (*note Built-in Variables::), it is
possible to detect when an empty data file has been skipped.  Similar to
the library file presented in *note Filetrans Function::, the following
library file calls a function named 'zerofile()' that the user must
provide.  The arguments passed are the file name and the position in
'ARGV' where it was found:

     # zerofile.awk --- library file to process empty input files

     BEGIN { Argind = 0 }

     ARGIND > Argind + 1 {
         for (Argind++; Argind < ARGIND; Argind++)
             zerofile(ARGV[Argind], Argind)
     }

     ARGIND != Argind { Argind = ARGIND }

     END {
         if (ARGIND > Argind)
             for (Argind++; Argind <= ARGIND; Argind++)
                 zerofile(ARGV[Argind], Argind)
     }

   The user-level variable 'Argind' allows the 'awk' program to track
its progress through 'ARGV'.  Whenever the program detects that 'ARGIND'
is greater than 'Argind + 1', it means that one or more empty files were
skipped.  The action then calls 'zerofile()' for each such file,
incrementing 'Argind' along the way.

   The 'Argind != ARGIND' rule simply keeps 'Argind' up to date in the
normal case.

   Finally, the 'END' rule catches the case of any empty files at the
end of the command-line arguments.  Note that the test in the condition
of the 'for' loop uses the '<=' operator, not '<'.

© manpagez.com 2000-2025
Individual documents may contain additional copyright information.