Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
How to Call an External Program Using awk
Awk is a powerful text processing tool that can automate various tasks such as data manipulation, filtering, and reporting. One of its most interesting features is the ability to call external programs from within an awk script, extending its functionality beyond text processing.
Understanding the system() Function
The system() function is used to execute external commands from within awk. The syntax is straightforward:
system(command)
The command argument is the external command you want to execute. When system() is called, awk passes the command to the shell for execution. The shell executes the command and returns an exit status to awk.
Basic External Program Execution
To call an external program using awk, specify the command as an argument to the system() function. Here's a simple example that executes the date command:
{
system("date")
}
When this script runs, the date command executes and displays the current date and time on the screen.
Passing Input to External Programs
You can pass input to external programs using pipes. For example, to sort a list of numbers using the sort command:
{
print "3<br>1<br>2" | "sort"
}
This example uses the print statement to generate a list of numbers, then pipes this data as input to the sort command, which sorts the numbers and displays the result.
Capturing Output from External Programs
You can store the output of an external program in a variable using the getline function. Here's how to capture the output of the ls command:
{
"ls" | getline output
print output
close("ls")
}
This script executes the ls command, captures its output in the output variable using getline, and then prints it. The close() function properly closes the command pipeline.
Passing Arguments to External Programs
You can construct commands dynamically by concatenating variables. Here's an example that converts a string to uppercase using the tr command:
{
str = "hello world"
cmd = "echo " str " | tr '[:lower:]' '[:upper:]'"
cmd | getline output
print output
close(cmd)
}
This example creates a command string that pipes text through tr for case conversion, then captures the uppercase result.
Error Handling
When calling external programs, it's important to handle potential errors. The system() function returns the exit status of the executed command zero indicates success, while non-zero indicates failure:
{
file = "myfile.txt"
cmd = "test -e " file
if (system(cmd) != 0) {
print "File does not exist"
} else {
print "File exists"
}
}
This script uses the test command to check file existence and handles both success and failure cases appropriately.
Advanced Examples
Using Variables in External Commands
You can incorporate awk variables into external commands. For example, creating a file with a variable-based name:
{
filename = "report_" strftime("%Y%m%d") ".txt"
cmd = "touch " filename
system(cmd)
print "Created file: " filename
}
Processing Multiple Lines of Output
To process all output lines from an external command:
BEGIN {
while (("ps aux" | getline line) > 0) {
if (NR > 1 && $11 ~ /bash/) {
print "Found bash process: " $2
}
}
close("ps aux")
}
Best Practices
| Practice | Description | Example |
|---|---|---|
| Always close pipes | Use close() to properly terminate command pipelines | close(cmd) |
| Check exit status | Verify command success before processing output | if (system(cmd) == 0) |
| Quote arguments | Use quotes around arguments containing spaces | "ls '" filename "'" |
| Handle empty output | Check if getline successfully read data | if (getline > 0) |
Conclusion
Awk's ability to call external programs through system() and pipes makes it a versatile tool for system administration and data processing tasks. By combining awk's text processing capabilities with external commands, you can create powerful automation scripts that handle complex workflows efficiently.
