AWK NF in Ubuntu 20.04:
The “NF” AWK variable is used to print the number of fields in all the lines of any provided file. This built-in variable iterates through all the lines of the file one by one and prints the number of fields separately for each line. To understand this functionality nicely, you will have to read through the examples discussed below.
Examples for Demonstrating the Usage of AWK NF in Ubuntu 20.04:
The following four examples have been designed in a way to teach you the usage of AWK NF in a very easy-to-understand way. All of these examples have been implemented using the Ubuntu 20.04 operating system.
Example # 1: Print the Number of Fields from Each Line of a Text File:
In this example, we wanted to print the number of fields or columns of each line or row or record of a text file in Ubuntu 20.04. For showing you the method of doing so, we have created the text file shown in the image below. This text file contains the rates of apples per kilogram from five different cities of Pakistan.
Once we had created this sample text file, we executed the following command to print the number of fields from each line of this text file in our terminal:
In this command, we have the “awk” keyword that shows that we are running an AWK command followed by the “print NF” statement that will simply iterate through each line of the target text file and will print the number of fields separately for each line of the text file. Finally, we have the name of that text file (whose fields are to be counted) which in our case is “AppleRatest.txt”.
Since we had the exact same number of fields for all the five lines of our text file, i.e., 2, the same number is printed as the number of fields for all the text file lines due to executing this command. This can be seen from the image below:
Example # 2: Print the Number of Fields from Each Line of a Text File in a Presentable Manner:
The output displayed in the example discussed above can also be presented nicely by displaying the line numbers and the number of fields of each line of the text file. Moreover, we can also separate the line numbers from the number of fields with any special character of our choice. We will be using the same text file that we have used for our first example to show you this. However, our command that is to be executed in this case will differ slightly, and it is as follows:
In this command, we have introduced the built-in AWK variable “NR” that will simply print the line numbers of all the lines of our target text file. Moreover, we have used three dashes, “—” as a special character to separate the line numbers from the number of fields of our provided text file.
This slightly modified output of the same text file is shown in the image below:
Example # 3: Print the First and Last Fields from Each Line of a Text File:
Apart from just counting the number of fields of all the lines of a provided text file, the “NF” special variable of AWK can also be used to extract the actual values of the last field from the provided text file. Again, we have used the very same text file that we have used for our first two examples. However, we want to print the actual values of our text file’s first and last fields in this example. For that, we executed the following command:
The “awk” keyword is followed by the “print $1, $NF” statement in this command. The “$1” special variable has been used to print the values of the first field or the first column of our provided text file, whereas the “$NF” AWK variable has been used to print the values of the last field or the last column of our target text file. You need to notice over here that when we use the “NF” AWK variable as it is, then it is used to count the number of fields of each line; however, when it is used with the dollar “$” symbol, then it will simply extract the actual values from the last field of the provided text file. The rest of the command is more or less the same as that of the commands that have been used for the first two examples.
In the output shown below, you can see that the actual values from the first and last fields of our provided text file have been printed on the terminal. You can see that this output is pretty much similar to the output of the “cat” command solely because we only had two fields in our provided text file; therefore, in a way, the contents of our whole text file were printed on the terminal as a result of executing the above-mentioned command.
Example # 4: Separate the Records with Missing Fields in a Text File:
At times, there are some records in a text file with certain missing fields, and you might want to separate those records from the ones that are complete in every aspect. This can also be done by using the “NF” AWK variable. For that, we have created a text file named “ExamMarks.txt” that contains the exam scores of five different students in three different exams along with their names. However, for the third exam, some of the students were absent because of which their scores were missing. This text file is as follows:
To distinguish the records with missing fields from the records with the complete fields, we will execute the command shown below:
This command is the same as the one that we have used for our second example. However, from the output of this command shown in the following image, you can see that the first and fourth records are complete, whereas the second, third, and fifth records contain missing fields.
The purpose of this article was to explain the usage of the “NF” AWK special variable. We first discussed briefly how this variable works, and after that, we elaborated this concept well with the help of four different examples. Once you understand all the shared examples well, you will be able to use the “NF” AWK variable to count the total number of fields and print the actual values of the last field of the provided file.