Print second-to-last column/field in `awk`

Viewed 172525

I want to print the second-to-last column or field in awk. The number of fields is the NF variable. I know that I should be able to use $NF, but I'm not sure how it can be used.

And this does not seem to work:

awk ' { print ( $NF-- )  } '
10 Answers
awk '{print $(NF-1)}'

Should work

awk ' { print ( $(NF-1) ) }' file

First decrements the value and then print it -

awk ' { print $(--NF)}' file

OR

rev file|cut -d ' ' -f2|rev
date
Sat Jul  9 11:57:36 EDT 2022

date | gawk '$_=$--NF'

     | mawk '$!--NF=$NF'
EDT
Related