gawk: File Checking

1 
1 10.3.3 Checking for Readable Data files
1 ---------------------------------------
1 
1 Normally, if you give 'awk' a data file that isn't readable, it stops
1 with a fatal error.  There are times when you might want to just ignore
1 such files and keep going.(1)  You can do this by prepending the
1 following program to your 'awk' program:
1 
1      # readable.awk --- library file to skip over unreadable files
1 
1      BEGIN {
1          for (i = 1; i < ARGC; i++) {
1              if (ARGV[i] ~ /^[a-zA-Z_][a-zA-Z0-9_]*=.*/ \
1                  || ARGV[i] == "-" || ARGV[i] == "/dev/stdin")
1                  continue    # assignment or standard input
1              else if ((getline junk < ARGV[i]) < 0) # unreadable
1                  delete ARGV[i]
1              else
1                  close(ARGV[i])
1          }
1      }
1 
1    This works, because the 'getline' won't be fatal.  Removing the
1 element from 'ARGV' with 'delete' skips the file (because it's no longer
1 in the list).  See also ⇒ARGC and ARGV.
1 
1    Because 'awk' variable names only allow the English letters, the
1 regular expression check purposely does not use character classes such
1 as '[:alpha:]' and '[:alnum:]' (⇒Bracket Expressions).
1 
1    ---------- Footnotes ----------
1 
1    (1) The 'BEGINFILE' special pattern (⇒BEGINFILE/ENDFILE)
1 provides an alternative mechanism for dealing with files that can't be
1 opened.  However, the code here provides a portable solution.
1