The awk
is a popular command line tool used to read content like a text file and search and select specific parts of the file. As Linux is a text-based operating system we may need to get some parts of the text files like the last column of the text file. The awk command can be used to print the last column of the text file or output via the command line interface.
Print Last Column
The awk command provides different file descriptors in order to specify different parts of a file. The $NF
is used to specify the last column of every line. Keep in mind that the column structure is not checked and only the last column is displayed for every line. The$NF is used with the print
statement like below.
$ awk '{print $NF}' /etc/fstab
Print Last Column For Multiple Files
The awk command can be used to print the last columns of multiple files easily with the help of the cat
command. In the following example, we print the last columns of the *.txt
files located in the ismail’s home directory.
$ cat /home/ismail/*.txt | awk '{print $NF}'
Print Last Column For Command Output
We can also print the last column of the command output. The |
operator is used to redirect command output to the awk command. In the following example, we print the last column of the mount
command.
$ mount | awk '{print $NF}'
Print Last Column By Specifying Column Number
If the file content or command output is very structured and the last column index number is static like the last column is the 8th column we can specify the column number to print the last column. In the following example, we print the last column which is also the 8th column.
$ mount | awk '{ print $8 }'