Print the Last Column from a File with the “awk” command-Linux Hint

“awk” is a really highly effective Linux command that can be utilized with different instructions in addition to with different variables. This command is actually used to learn the content material of a file. The method of file studying has by no means been this a lot simpler as it’s with this environment friendly command. File studying and writing are very repeatedly used particularly in case you are a programmer. The learn file can then be used to course of its content material to switch it, and even to easily print it.Nevertheless, there are conditions during which you don’t intend to learn the whole content material of that file quite you’re solely involved with a selected portion of that file. In such a state of affairs, it’s extremely not beneficial to learn the whole file since it’s going to occupy further house and also will take an extended time for processing quite it is best to immediately hit that specific portion of that file. On this article, we purpose to stroll you thru the totally different strategies of utilizing the “awk” command to print the final column from a file.

Totally different Methods of utilizing the “awk” Command to Print the Final Column from a File:

There are two totally different strategies during which we are able to use the “awk” command to print the final column from a file. Though, these two strategies can be utilized interchangeably as a result of in spite of everything, they render the exact same output, nevertheless, the primary technique is extra appropriate for the state of affairs the place you realize the precise variety of columns of a file and the second technique is useful when the full variety of columns is unknown. Allow us to check out each these strategies one after the other.

Observe: We’ve used Linux Mint 20 for demonstrating each of those strategies, nevertheless, you’re free to pick out any taste of Linux of your selection.

Technique # 1: When you realize the Complete Variety of Columns of a File:

This technique can be utilized when you realize the precise variety of columns in a file e.g. 3, 5, 10, and so forth. For demonstrating this technique, the next steps must be carried out within the specified order:

  • Create a textual content file with any title of your selection in your Residence folder. On this state of affairs, we now have named it as awk.txt. Now double click on on this file to open it and kind the info proven within the picture beneath in your textual content file. There are a complete of three totally different columns and the columns are separated from one another by tab house. You can too sort any random information of your selection.

  • After creating this file, you must put it aside and shut it. Now launch the terminal in Linux Mint 20 by clicking on its icon situated on the taskbar. The Linux Mint 20 terminal can be proven within the following picture:

  • Now sort the below-mentioned command in your terminal after which press the Enter key to execute it:

$ awk ‘{print $ColNum}’ file.txt

Right here, it’s worthwhile to change ColNum with the column variety of the final column. For instance, the textual content file that we now have created has a complete of three columns which signifies that the column variety of the final column shall be 3. Since our purpose is to solely print the final column of this file, so we now have changed ColNum with 3. Each time a column quantity is proceeded by the “$” image, then which means that we wish to entry the values of that column. Furthermore, you even have to exchange “file” with the precise title of your textual content file. In our case, the file title was awk.txt.

  • As quickly as you’ll hit the Enter key all of the values of the final column of the file awk.txt will seem in your terminal as proven within the following picture. The awk command will learn this column whereas the print command shall be chargeable for displaying its values on the terminal.

Technique # 2: When the Complete Variety of Columns of a File is Unknown:

This technique is usually used once you have no idea concerning the complete variety of columns in a file. For printing the final column of a file by way of this technique, you’ll have to proceed as follows:

  • We’re utilizing the identical textual content file that we now have created for the tactic above. All we now have to do is to launch the terminal after which sort the next command in it:

$ awk ‘{print $(NF)}’ file.txt

Right here, NF is a variable whose job is to explicitly print the final column from a file. For instance, if there are 10 or 20 or much more columns in a file however you have no idea their actual quantity and you continue to wish to entry the final column, then this variable can show to be very useful for you. Once more, it’s worthwhile to change “file” with the title of your textual content file. In our case, since we now have used the identical file as that of Technique # 1, due to this fact, we now have changed file.txt with awk.txt.

  • After typing this command in your terminal, it’s worthwhile to run it by urgent the Enter key. You’ll discover that the output of this command is similar to the output of the command used within the technique above i.e. the final column of the textual content file which contained the 12 months information has been efficiently printed on the terminal.


On this article, we gave you the steerage about printing solely the final column from a file whereas making use of the “awk” command. There are much more elements of this command which could be explored intimately i.e. this command can be used along side different totally different instructions to render a unique type of output. Nevertheless, for the scope of in the present day’s dialogue, our concern was solely to exhibit its utilization for printing the final column from a file. We offered to you the 2 other ways of doing the identical factor. Now it fully is dependent upon your state of affairs that which of those strategies you select to observe.

awk print first column csv,awk ‘(print first row),what does a pipe do?,awk print line if column matches,awk match pattern in column,awk command to print columns with delimiter,awk print second to last column,awk ‘(print last column minus 1),awk ‘(print last row),awk ‘(print last n columns),awk ‘(print last field delimiter),awk ‘(print last column separated by comma),awk ‘(print all fields after),awk print the whole file,awk print first n columns,awk to print last column,awk print all fields with separator,awk ‘(print all fields separated by comma),awk vs sed,awk cheat sheet,awk script file,awk vs grep,awk variables,awk regex,awk select rows,awk fnr,awk print all columns,awk skip first line,unix command to extract specific column from a file,awk command in linux,awk ‘(print column matching pattern),awk command to print 3rd column,awk last column,awk print multiple columns

You May Also Like