Pete's Linux Advent Calendar 2008

The 2nd day

Retrieve the last field of a file

You probably know that with awk you can extract a column of a file. Assume a file timesheet contains this:
hours spent on task y: 2
hours for task x: 3
task z: 4
hours for u: 5
As you see each line contains a different number of fields. So to extract the last field you cannot use a fixed column number. But awk sets the variable NF that you can use like this:
$ awk '{print $NF}' timesheet
2
3
4
5

Pete's Linux Advent Calendar 2008

Valid HTML 4.01!

Comments? send mail to pkruse@arcor.de my monogram