getting the last parameter through awk & split

Hi,

I would like to know how to extract and get the value of the last element in an array after split.

Let's say i have an input of

aaa mykeyword_111 abc/def/ghi/mno
bbb none ttt.aaa.ccc
ccc mykeyword_222 ppp/qqq/rrr/ss

I expect the output to be like this way.

aaa 111 mno
ccc 222 ss

I am trying to write in such a way below.

awk '$2 ~/mykeyword/ {
  {split($2, arr, "_")}
  {split($3, arr2, "/") } <---- i wanna get the last element here
  {print $1","arr[2]","arr2[THE_LAST_ELEMENT]}
}' logfile

I tried arr2[NR] or arr2[$NR] but still failed..

Thank you.

echo 'SOME|DUMMY|DATA'|awk '{n=split($0,a,/[|]/);print "LAST ELEMENT : " a[n]}'
LAST ELEMENT : DATA

Get it?

Another approach:

awk '$2 ~ /mykeyword/{sub(".*_",x,$2); sub(".*/",x,$NF);print $1, $2, $NF}' file

Hi,

How about this..?


awk -F'[ _/]' '$2 ~/mykeyword/{print $1","$3","$NF;}' file

awk 'BEGIN{FS="[ _/]";OFS=",";}$2 ~/mykeyword/{print $1,$3,$NF;}' file

Cheers,
Ranga :slight_smile:

Thanks! never realize that i could get the number of elements in such a way...
Learnt a new thing today! thank you!

It works!
Thank you!
This approach is a bit high level to me... ^^; but learnt a new thing today!
Thank you!

Good! I just know taht we could specify multiple FS... thank you!