- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to Call an External Program Using awk
Introduction
Awk is a powerful text processing tool that can be used to automate various tasks such as data manipulation, filtering, and reporting. One of interesting features of awk is its ability to call external programs from within script. In this article, we will explore how to call an external program using awk.
Understanding System() Function
The system() function is used to execute external commands from within awk. syntax of system() function is as follows −
system(command)
The command argument is external command that we want to execute. When system() function is executed, awk passes command argument to shell for execution. shell then executes command and returns exit status to awk.
Calling an External Program
To call an external program using awk, we need to specify command that we want to execute as argument to system() function. For example, let's say we want to execute date command from within an awk script. We can do this as follows −
{ system("date") }
In this example, we are calling date command using system() function. When script is executed, date command is executed, and current date and time are displayed on screen.
Passing Input to an External Program
We can pass input to an external program using system() function. For example, let's say we want to sort a list of numbers using sort command. We can do this as follows −
{ print "3
1
2" | "sort" }
In this example, we are using print statement to generate a list of numbers (3, 1, 2). We are then using pipe symbol (|) to pass this list of numbers as input to sort command. sort command then sorts numbers and returns result to awk.
Storing Output of an External Program
We can store output of an external program in a variable using getline() function. getline() function is used to read a line of input from a file or a command. We can use getline() function to capture output of an external program. For example, let's say we want to capture output of ls command in a variable. We can do this as follows −
{ "ls" | getline output print output }
In this example, we are using system() function to execute ls command. We are then using getline() function to capture output of ls command in output variable. Finally, we are using print statement to display output of ls command.
Passing Arguments to an External Program
We can pass arguments to an external program using system() function. For example, let's say we want to convert a string to uppercase using tr command. We can do this as follows −
{ str = "hello world" cmd = "echo " str " | tr '[:lower:]' '[:upper:]'" cmd | getline output print output }
In this example, we are using echo command to pass string "hello world" as input to tr command. We are then using tr command to convert string to uppercase. Finally, we are using getline() function to capture output of tr command in output variable.
Handling Errors
When calling an external program using awk, it's important to handle errors that may occur during execution. We can check exit status of an external program using system() function. exit status is a number that indicates success or failure of command. A value of zero indicates success, while a non-zero value indicates failure.
For example, let's say we want to check if a file exists using test command. We can do this as follows −
{ file = "myfile.txt" cmd = "test -e " file if (system(cmd) != 0) { print "File does not exist" } else { print "File exists" } }
In this example, we are using test command to check if file "myfile.txt" exists. We are then using system() function to execute test command. If exit status of test command is non-zero, we print "File does not exist". Otherwise, we print "File exists".
Advanced Examples
Filtering Output of an External Program We can filter output of an external program using awk. For example, let's say we want to display only first line of output of ps command. We can do this as follows −
{ cmd = "ps" cmd | getline output close(cmd) split(output, lines, "
") print lines[1] }
In this example, we are using getline() function to capture output of ps command in output variable. We are then using split() function to split output into an array of lines. Finally, we are using print statement to display first line of output.
Using awk Variables in External Commands We can use awk variables in external commands. For example, let's say we want to create a file with a name based on a variable using touch command. We can do this as follows −
{ filename = "myfile.txt" cmd = "touch " filename system(cmd) }
In this example, we are using filename variable in touch command to create a file with name "myfile.txt".
Using awk to Format Output of an External Program We can use awk to format output of an external program. For example, let's say we want to display output of df command in a more readable format. We can do this as follows −
{ cmd = "df -h" cmd | getline output close(cmd) printf "%-20s%-10s%-10s%-10s%-10s%s
", "Filesystem", "Size", "Used", "Avail", "Use%", "Mounted on" split(output, lines, "
") for (i=2; i<=length(lines); i++) { printf "%-20s%-10s%-10s%-10s%-10s%s
", $1, $2, $3, $4, $5, $6 } }
In this example, we are using printf statement to format output of df command. We are printing a header line with column headings, followed by output of df command with each column aligned to a fixed width.
Conclusion
In this article, we have explored how to call an external program using awk. We have seen how to pass input to an external program, capture output of an external program, and handle errors that may occur during execution. By using awk's ability to call external programs, we can automate various tasks and make our scripts more powerful and flexible.