how to access last index of array from split function inside awk? how to access last index of array from split function inside awk? shell shell

how to access last index of array from split function inside awk?


If your problem is exactly as the example in your question, take the answer from @muzido, $NF will give you the last field.

If you just want to know the last element of an array by split():

split() function will return you how many elements it has just "splitted", test with your code: awk '{print split($1,A,".")}' file you will see the number. Then you can just use it by:

awk '{n=split($1,A,"."); print A[n]}' file # n is the length of array A


If you have GNU awk, you can try the function length on a array:

awk '{split($1,A,"."); print A[length(A)]}'


Why not:

$ awk '{print A[split($3,A,".")],$0}' input.txt

Hope it helps!