Next: , Previous: , Up: Data file Management   [Contents][Index]

10.3.3 Checking for Readable Data files

Normally, if you give awk a data file that isn’t readable, it stops with a fatal error. There are times when you might want to just ignore such files and keep going.71 You can do this by prepending the following program to your awk program:

# readable.awk --- library file to skip over unreadable files

    for (i = 1; i < ARGC; i++) {
        if (ARGV[i] ~ /^[a-zA-Z_][a-zA-Z0-9_]*=.*/ \
            || ARGV[i] == "-" || ARGV[i] == "/dev/stdin")
            continue    # assignment or standard input
        else if ((getline junk < ARGV[i]) < 0) # unreadable
            delete ARGV[i]

This works, because the getline won’t be fatal. Removing the element from ARGV with delete skips the file (because it’s no longer in the list). See also Using ARGC and ARGV.

Because awk variable names only allow the English letters, the regular expression check purposely does not use character classes such as ‘[:alpha:]’ and ‘[:alnum:]’ (see Using Bracket Expressions).



The BEGINFILE special pattern (see The BEGINFILE and ENDFILE Special Patterns) provides an alternative mechanism for dealing with files that can’t be opened. However, the code here provides a portable solution.