How can i do this using awk?
Example -
awk '{split($1,A,"."); print A[-1], $1, $2, $3, $4}' Sample input and output.
Input
123 456 abc.def.ghi 789 321 654 qaz.wsx.edc.rfv 987 Output
ghi 123 456 abc.def.ghi 789 rfv 321 654 qaz.wsx.edc.rfv 987 14 Answers
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 3If you have GNU awk, you can try the function length on a array:
awk '{split($1,A,"."); print A[length(A)]}' 4Why not:
$ awk '{print A[split($3,A,".")],$0}' input.txt Hope it helps!
Kent already gave you the split() answer but you don't need split creating/using an array for this, e.g. with GNU awk for gensub():
$ awk '{print gensub(/.*\./,"",1,$3), $0}' file ghi 123 456 abc.def.ghi 789 rfv 321 654 qaz.wsx.edc.rfv 987