Hello
how to find last occurence of a string
for example in the following I want last occurence of '-' i.e. position 12
str="aa-bbb-cccc-ddd-ee"
my pupose is to get the string 'ee'
Thanks and Regards
Chetanz
Hello
how to find last occurence of a string
for example in the following I want last occurence of '-' i.e. position 12
str="aa-bbb-cccc-ddd-ee"
my pupose is to get the string 'ee'
Thanks and Regards
Chetanz
echo "aa-bbb-cccc-ddd-ee" | awk -F"-" '{print $NF}'
Many Thanks Pravin27
Sorry i misphrased the question
I got there but stumbled at getting last position
could you please advise how to get the "position" of last '-' in the string?
Thanks and Regards
Chetanz
here you go
echo "aa-bbb-cccc-ddd-ee" | awk -F"-" '{print length($0)-length($NF)}'
Assuming you're using a standards conforming shell (such as bash or ksh), this will also work:
str="aa-bbb-cccc-ddd-ee"
end=${str##*-}
echo "Last - is in column $((${#str} - ${#end}))"
and only uses shell built-ins.
With your sample string, it produces:
Last - is in column 16
start=${str%-*}
echo ${#start}
15
This is OK, as the shell's string index starts with 0:
echo ${str:${#start}:1}
-