Reputation: 324
I want to find the sum of the second last column of a file that has variable number of columns.
I know that if I knew that exact number of the column, I would be able to do -
awk '{s+=$1} END {print s}' mydatafile
for column 1
. But not sure how I would do it if I didn't know the exact column number.
Upvotes: 1
Views: 1381
Reputation: 158280
The special variable NF
refers to the number of fields per line in awk
. To get the second last field you can access $(NF-1)
:
awk '{s+=$(NF-1)} END{print s}' file
Upvotes: 3