1. Overview

Awk is a robust text processing tool commonly used in Linux systems to alter and analyze structured data. Awk lacks a direct built-in file existence checking feature among its many capabilities.

In this tutorial, we’ll outline and explain how to check if a file exists within Awk.

2. Using Awk in Combination with Shell Commands

The system() function enables Awk to integrate smoothly with shell commands. We can, therefore, check for file existence by utilizing the ls command within Awk:

BEGIN {
    filename = "file.txt"  # Replace 'file.txt' with the file name you want to check
    command = "ls " filename " 2>/dev/null"

    if (system(command) == 0) {
        print "File exists"
    } else {
        print "File does not exist"
    }
}

Here, the variable filename stores the name of the file for verification. The system() function executes the ls command with the specified filename, redirecting error messages to /dev/null. If the file exists, ls returns success (0), and the script prints File exists. Otherwise, it prints File does not exist.

Additionally, this method runs the ls shell command inside the Awk script and decodes its output to determine if the given file exists. Using the system() function in Awk extends its capability to interact with the system environment, enabling the execution of shell commands and conducting file existence checks within the scripting environment.

3. Using Awk With the getline Function

Another method of checking if a file exists in Awk is by using the getline function in Awk to check for file existence indirectly. Awk’s getline function makes reading input from files or other sources easier. As a result, it indirectly serves the purpose of determining the existence of a file within an Awk script:

BEGIN {
    filename = "file.txt"  # Replace 'file.txt' with the file name you want to check
    if ((getline < filename) > 0) {
        print "File exists"
    } else {
        print "File does not exist"
    }
    close(filename)
}

Here, we use the getline function to attempt to read the file. If the getline function returns a value larger than zero, it confirms the file’s existence, which indicates a successful read. If not, it is non-existent.

4. Using Awk’s ARGV Array

Awk provides an ARGV array that stores command-line arguments, including file names. Although it doesn’t directly check file existence, we can exploit it to indirectly determine if a file exists by accessing it through ARGV.

Hence, if an attempt to access the file throws an error, it indicates the file’s non-existence:

BEGIN {
    filename = "file.txt"  # Replace 'file.txt' with the file name you want to check

    if (ARGIND == 1) {
        print "File exists"
    } else {
        print "File does not exist"
    }
}

In this example, the if condition checks the value of ARGIND, which is the index of the current file that Awk is processing. If ARGIND = 1, it means that the file supplied in the filename as the first file parameter was successfully accessed. As a result, the script prints File exists to indicate that the file exists. If ARGIND is not equal to 1, it indicates that the file was not accessed as the first parameter and doesn’t exist.

5. Utilizing the stat Command

We can infer the file’s existence by using system() to run stat and capture its output within Awk:

BEGIN {
    filename = "file.txt"  # Replace 'file.txt' with the file name you want to check

    command = "stat " filename " >/dev/null 2>&1"
    if (system(command) == 0) {
        print "File exists"
    } else {
        print "File does not exist"
    }
}

In this instance, the system() function executes the stat command for the specified file. A successful command (0 exit status) confirms the file’s existence. Otherwise, it’s non-existent.

6. Conclusion

Awk is a versatile text processing tool that lacks a direct built-in file existence checking feature. In this article, we leverage various options to determine the existence of a file within an Awk script.