2.0 Environment Setup and Verification
2.1. Foundational Steps
Before writing our first script, it is crucial to ensure that the AWK interpreter is correctly installed and accessible from the command line. This foundational step verifies that the system is ready to execute AWK programs.
2.2. Installation via Package Managers
On most GNU/Linux systems, GNU AWK (GAWK) is pre-installed by default. You can verify its presence by executing the which command, which should report the path to the executable:
[jerry]$ which awk
/usr/bin/awk
If AWK is not installed, you can easily add it using your distribution’s package manager.
- Debian-based Systems (e.g., Ubuntu): Use the Advanced Package Tool (APT) to update your package lists and install gawk.
- RPM-based Systems (e.g., CentOS, Fedora): Use the Yellowdog Updater, Modified (YUM) package manager.
2.3. Installation from Source Code
For advanced users or on systems without a package manager, AWK can be compiled and installed directly from its source code. This process is standard for most GNU software.
- Download the Source Code: Use a command-line utility like wget to download the source tarball from the official GNU repository.
- Extract the Archive: Decompress and extract the downloaded file.
- Configure the Build: Change into the newly created source directory and run the configure script. This script checks your system for dependencies and prepares the build environment.
- Compile the Code: After configuration completes, it generates a Makefile. Use the make command to compile the source code into an executable.
- Run Tests (Optional): You can run the included test suite to verify that the build is clean and functioning correctly.
- Install the Program: Finally, install the compiled binary to the appropriate system directory. This step typically requires super-user privileges.
After installation, you can verify its success using the which awk command as shown previously.
With the environment now properly configured, we can delve into the fundamental workflow of an AWK program.