The awk command is used to extract data from a file and print specific contents. It is quite often used to restructure the data and construct reports. Its name is derived from the last names of its creators: Alfred Aho, Peter Weinberger, and Brian Kernighan. Its main features include the following:
- It is an interpreted programming language similar to C
- It is used for data manipulation in files, and for retrieving and processing text from files
- It views files as records and fields
- It has arithmetic and string operators
- It has variables, conditional statements, and loops
- It reads from a file or from a standard input device and outputs to a standard output device such as a Terminal
Its general invoking syntax is as follows:
$ awk '/pattern/{command}' <filename>
The printing of a selected column or row from a file is the basic task generally performed using awk.
In the following example, the awk command is used to print the contents of a file line by line until the end of the file is reached:
$ awk '{ print $0}' /etc/passwd
In the following example, awk command is used to print the first field (column) of the line containing the username student. Here -F option is used to set the field separator as :.
$ awk -F: '/student/{ print $1}' /etc/passwd
In the following example, the awk command is used to print selective fields from the line containing the matching pattern in file /etc/passwd:
$ awk -F: '/student/{print "Username :", $1, "Shell :", $7}' /etc/passwd