Ubuntu

AWK Features and Examples

“AWK is a very useful Linux command that acts as an extremely versatile scripting tool. It allows you to write mini scripts in the form of terminal commands with which you can easily manipulate the files in Linux. It allows you to define certain patterns within the command that can be executed over the provided file. In this article, we will discuss some of the most significant features of the AWK command in Linux, and then we will head on to a few examples that make use of this command.”

Features of the AWK Command

The AWK command in Linux offers different features. However, the following are the most noteworthy features of this Linux command:

  • It can easily scan a file line by line.
  • It can separate out the different fields of a file since it splits the file into different columns while reading it.
  • It can perform different actions on a file, such as text search, text comparison, etc.
  • It can also perform arithmetic and logical operations on a file.
  • It can conveniently work with different regular expressions.
  • It can also be used to print some information about the file, such as its name and the details of the data that it contains.

Examples of the AWK Command

Some of the examples of using the AWK command in Linux are explained below. However, before moving on to these examples, we would like to share with you the test file that we will be using in all of these examples. This file is shown in the following image:

Example # 1: Using the AWK Command for Printing a Single Column of a File

In this example, we wish to print a single column of the text file shown above. For that, we have used the AWK command as shown below:

$ awk ‘{print $2}’ testfile.txt

This command will print the second column of the test file, i.e., the names of the cities of the UK on the terminal, as shown in the following image:

Example # 2: Using the AWK Command for Printing Multiple Columns of a File

Now, we will use the AWK command for printing multiple columns of the file shown above at the same time on the terminal. For that, we will use the AWK command as shown below:

$ awk ‘{print $2, $3}’ testfile.txt

This command will print the second and third columns of the test file, i.e., the names of the cities of the UK and their respective area codes on the terminal, as shown in the following image. However, if you will skip the comma between the column numbers in the above-mentioned command, then these columns will be printed without any space in between.

Example # 3: Using the AWK Command With Logical Operators

In this example, we want to pair up the AWK command with the logical operators in Linux. We want to define different conditions to get the desired output. For that, you can take a look at the command shown below:

$ awk ‘$1>3 && $3>0000 {print $2}’ testfile.txt

This command will only print those entries from the second column of our file where the value of the first column will be greater than “3,” and that of the third column will be greater than “0000,” as shown in the following image. In the same manner, you can also use the “||” operator with the AWK command to define any condition of your choice.

Example # 4: Using the AWK Command for Printing the File Information

Now, we want to use the AWK command for printing the file-related information on the terminal, such as the name of the file, the total number of rows and columns, etc. For that, we have used the AWK command shown below:

$ awk ‘END{print “The name of the file is “, FILENAME, “It has “, NF, “columns and “, NR, “rows”}’ testfile.txt

This command will print the name of the file and the total number of rows and columns that it contains on the terminal, as shown in the following image. “NF” and “NR” are the built-in variables of the AWK command that refer to the total number of fields and the total numbers of records, respectively, present in a file. Again, if you will skip the commas in this command, then your output will look extremely messy as it will be printed without any spaces on the terminal.

Conclusion

This tutorial was designed to explain to you the usage of the AWK command in Linux. After introducing you to this powerful tool, we discussed some of its most important features, after which we shared a few examples of this command with you. Once you go through these examples, you will be able to realize the true strength of this Linux command.

Similar Posts