AWK: How to print last field or column from a file or text in Linux?

Today we will look at one interesting tip of AWK programming language or command.

AWK is very interesting and useful command of Linux which can help us to perform operations on file columns, compare them, do calculations and print results.

In this post we will look one small tip to print last field of specified input using awk.

Print Last field from input text/file

[root@nglinux ~]# echo "1 2 3 4 5 6 7 8" | awk '{print $NF}'
8
[root@nglinux ~]# 

What is $NF here ?
NF is a special AWK bultin which gives us the total number of fields in the passed record.

In the above example NF will return 8 since there are 8 columns and hence it prints the last column.

Awk NF builtin is very useful for validating if all fields exist in a record.

I hope you liked the tip.

We will look all AWK Builtins in future articles to understand them.

Leave a Reply

Your email address will not be published.