Counting fields backwards from end of line

Hello,

file.dat

1,2,3,4,5,6,7,8
1,2,3,4,5,6
1,2,3,4

I need to output the value in the third column from the end:

6
4
2

Thanks!

awk -F, '{print $(NF-2)}' file.dat
1 Like

This doesn't validate if you actually have an element in the 3rd position from the end, One example:

$ declare -a array=( 1 2 3 4 5 6)
$ c=${#arrray[@]}; e=$c-3; echo ${array[$e]}
4
1 Like