Print second last column/field in awk

Brian G picture Brian G · Jan 19, 2010 · Viewed 146.2k times · Source

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

And this does not seem to work:

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

Answer

Chris Kannon picture Chris Kannon · Jan 19, 2010
awk '{print $(NF-1)}'

Should work