Using the “awk” Command to Print the Last Column from a File
Sometimes there comes a situation when we do not need to read the complete file, we just need some part of the file to be displayed. In this situation, we try to find some efficient ways to get them without going through the whole file content. The “awk” command is the one.
The “AWK” command is a command line utility used in Linux to conduct searches with different patterns and for processing. It is a command that allows a coder to code small but efficient lines of code in the form of statements that initiate patterns of text that are used for conducting searches in every part of a document. So, it is also a vastly used command for text processing.
With the help of the “awk” command, a programmer can pick up data, and pieces of a specific text based on the pattern provided to the command. AWK command can scan and search files line by line, distribute and partition each line passed as input into fields, compare and match fields with input lines, and perform instructions specified by the programmer on matched lines. AWK commands can be used for the production of reports in specific formats and for changing data inside the files.
Following is the syntax of the AWK command:
In this command, the parameter “action” in the syntax is used to define the action that our command will be to performed. After that, we will pass the name of our file as a parameter on which we are going to operate.
Example no. 1:
In this example, we will try to print the nth or the last column of the file using the awk command. Let us first create a new file that contains some data related to employees. To create a new file, we will run the below-displayed command:
After running the above command, it will create a new file in our home directory. It is not necessary to create a file in the home directory you can create it anywhere in your system. Now, we will check whether the file is created or not. For that, we will simply check the home directory. As shown in the snippet below, we have created a new file named employee.txt.
Now, we will add some content to the file using a notepad, as shown in the snippet below. We have added five employees’ data in it which includes the name, designation, and salary of the employee. The data is shown below that is stored in the file “employee.txt”.
Now, the file is successfully created with the content and to get the last column of the file using the awk command. For that, we will run the command mentioned below:
In the command above, we passed the print statement along with the “$NF” keyword which means the awk command will instruct the compiler to print the “$NF” which indicates the nth field of the file. The last one “employee.txt” is the name of the file whose last column will be printed. Pressing the enter key, we will run the command on which the output displayed below is generated on the terminal in which the salary of the employees is displayed.
Example no. 2:
In this example, we will read the last column of the file but this time we are using another method which is mostly used when the number of columns is known by the user. Now, let us first create a new file that contains the data of five students. It includes the name of the students, their classes, and the marks that they obtained.
When we run the command above, it will create a new file and opens it automatically in a text editor. Now, we will add the data of students in the file and by pressing “Ctrl + x”. We will save the file after adding the content to it. In our case, we added data of five students in it. By saving the file, we will move towards our main objective which is to print the last column of the file. Below is the content of the file “student.txt”.
Now we will run the awk command to print the last column of the file. Let’s suppose this time we know the columns of the file and we want to print the last column of it. For that, we will use the awk command along with the column number that will be printed on the terminal. As we know that there are only three columns in our file that are the name of the students, their classes, and the marks. To print the last column, we will run the below-shown command:
In this command, we passed “$3” along with the print command which means “awk” will instruct the compiler to print the third column of the file named “student.txt”. When we run this command, it will print the following output in which the marks of the students are printed. We can also compare the output with the file snippet that is displayed above. The file contains only three columns and the last column of the file contains the marks of the students. This command can be used to print any column of the file; it is not only specified to print the last column. By changing the “$3” sign to “$2”, we can print the second column. After running the command above, we will get the output displayed below.
We have studied an “awk” command that is widely used to directly hit the content of the file that is needed. Then, we implemented multiple examples to read the specified last column of the files using the awk command. You can implement more examples to get a better idea of how the awk command can be used.