What is AWK NF?

The “AWK” function is a Linux utility used for text analysis that offers strong data control. To use the Linux computer program and the “AWK” function, users can evaluate, modify and generate the prepared results. The AWK NF variable is used to indicate how many files, items, or other items are in a line of any file.

How AWK NF works

The “AWK” program works with programs that contain the rules, which consist of various patterns and actions that are performed on text that matches the pattern. A pattern and an action together form a rule. The entire “AWK” program is then enclosed in a single quote.

Now we’ll just use a simple one example to demonstrate what happens when we type “who” into the Ubuntu 20.04 terminal. This command displays information about the user and the current time and date.

[email protected]:~$ who

We run this command by clicking Enter on the keyboard, which prints the date and time as “2022-12-08” and “15:06” respectively, before posting some spaces and the username as “Linux”. .

By default, AWK interprets a field as a string enclosed in spaces at the beginning and end of a line. Here we demonstrate how to write a command using the “AWK” parameters and “who” to get the first field containing the dollar sign and the number 1 ($1). If we want to retrieve the first field from a text file or other data, we use the dollar sign “$1” with a . For this we use the command in which we first write “who”. Then we use this “|” bar. Then we set spaces by pressing the space bar on the keyboard. We then write “awk”. Then we use an inverted comma followed by curly brackets. Finally, we use the “print” word and the “$1” symbol inside those brackets.

[email protected]:~$ who | awk ‘{print $1}’

As you can see in the previous output image, if we just use “who” in the terminal and run that, it will show the username along with the required information, including the date and time. However, since we use “print $1” with “AWK” in the previous command, only the first field will be printed because “$1” is used to print the first field of the file, among other things.

Create a text file on the desktop with the terminal and print the entire content with “AWK”

Since the entire content of a file is printed on the end device with “AWK”, we first create a text file and print out the information in the file on the end device. Here we use the terminal to create a text file on the desktop. To do this, we write the command in which we write “nano” as a text editor. Then we use the symbol (~) followed by the slash “/”. Then we write “desktop” because we are creating a file on the desktop, followed by the slash “/”. Again we then write the name of the file we want to create. Finally, we create a file called “textfile” with the extension “.txt” since it’s a text file.

[email protected]:~$ nano ~/Desktop/textfile.txt

Therefore, a terminal will show us a blank screen when we type this command, as shown in the image below:

We enter the data by typing it into the terminal that we want to save in the text file. To do this, we create five lines. The first of these contains the word “Hello”. The second contains “hey”. The third contains “hi”. The fourth contains two elements, “Goodbye” and “123”. And the 5th row is blank as shown in the following image:

If we press “Ctrl-x” after entering the data in the “textfile.txt” file, the choice “Yes” or “No” will appear on the screen. We know we want to save the file. So we press “ctrl-y” to save the file and the file will be created successfully.

The following image shows the file shown:

As we know, the file is saved on the desktop. We must first add a directory to the desktop using the “cd Desktop/” command and then press Enter.

[email protected]:~$cdDesktop/

Now we use the “AWK” function to get the content of the newly created “textfile.txt” file and display it on the screen. For this we use the command in which we first write “awk”. Then we use the quotation marks inside. We put the curly brackets and write “print $0”. After that we use a space. Finally, we write the file name “textfile.txt”.

[email protected]:~/Desktop$ awk ‘{print $0}’ textfile.txt

Now when we run this command, the contents of the text file will be displayed as you can see in the image below. The first line of this file contains the word “Hello” followed by “Hey”. The word “hi” appears on the third line. The fourth line lists two items. And the fifth line is empty.

View the contents of the file with line numbers

Using the “NR” variable in the command, we output the contents of the file along with the line number in the next part. For this we use the command in which we first write “awk”. Then we use a double quote after a space. In it we place the curly brackets. Then we write pressure. Finally, we write “NR”. After that we put a comma and write “$0”. Then we leave the parenthesis and the quotation mark. Finally we write the file name “textfile.txt”.

[email protected]:~/Desktop$ awk ‘{print NR, $0}’ textfile.txt

As you can see in the result, the contents of the file are displayed with line numbers showing that it contains “5” lines.

Displaying the total amount of data per line with NF

In this section, we show the total number of items in each row. For this we will use the command where we will first write “awk” and then we will follow the same steps as in the previous command, but we will use the “NF” variable after we have written the “print” parameter. Next we close the curly brackets and quotation marks. Then we write the file name “textfile.txt”.

[email protected]:~/Desktop$ awk ‘{print NF}’ textfile.txt

The number of elements present in the file is now displayed in the output. The first three rows contain “1” elements. The fourth row contains “2” items. And the last line in the file is empty. Therefore, it shows “0”.

Eliminate the blank lines

In this section, we will delete the blank line from the file. For this we use the command in which we first use “awk”. Then we use “NF>0” which is the condition saying that the line will be printed if the number of elements is greater than “0”. Otherwise the line will not show it. Then we use the same procedure as in the previous section.

[email protected]:~/Desktop$ awk ‘NF>0 {print $0}’ textfile.txt

Because the fifth line of the file is empty, it doesn’t appear on the screen. Instead, only the rows that contain the items or items are displayed. Therefore, the first four lines of the file are displayed because they contain the data, while the last line is deleted because it contains no data.


In this article, we have talked about how you can use the “AWK NF” feature in Ubuntu 20.04 to print the data or information of the text files. In the first step, we showed how the “AWK” function works. In the second step we created a text file on the desktop and used the various parameters of the “AWK” function such as “NR” to print the lines of the file with numbers. Next, we used “NF” to count the number of items present on each line of the file. In the last step, we deleted the empty line from the file by applying a condition with “NF”.

Related Posts