Find last occurrence of a character in a string

Tags
shell scripts

Login to Reply

 
Thread Tools Search this Thread
# 1  
Old 05-30-2013
Find last occurrence of a character in a string

Hello

how to find last occurence of a string

for example in the following I want last occurence of '-' i.e. position 12

Code:
str="aa-bbb-cccc-ddd-ee"

my pupose is to get the string 'ee'

Thanks and Regards
Chetanz
# 2  
Old 05-30-2013
Code:
echo "aa-bbb-cccc-ddd-ee" | awk -F"-" '{print $NF}'

# 3  
Old 05-30-2013
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
# 4  
Old 05-30-2013
here you go
Code:
echo "aa-bbb-cccc-ddd-ee" | awk -F"-" '{print length($0)-length($NF)}'

# 5  
Old 05-30-2013
Assuming you're using a standards conforming shell (such as bash or ksh), this will also work:
Code:
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:
Code:
Last - is in column 16

# 6  
Old 05-30-2013
Code:
start=${str%-*}
echo ${#start}
15

This is OK, as the shell's string index starts with 0:
Code:
echo ${str:${#start}:1}
-

Login to Reply

|
Thread Tools Search this Thread
Search this Thread:
Advanced Search

Similar Threads More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
awk or other way to find out number of occurrence of 7th character rveri Shell Programming and Scripting 10 02-04-2017 06:33 AM
To find nth position of character in string Jonty Immortal Shell Programming and Scripting 2 05-13-2014 06:29 PM
[Solved] Awk: count occurrence of each character for every field beca123456 UNIX for Dummies Questions & Answers 4 03-03-2014 07:14 AM
How to replace particular occurrence of character in between a delimiter? H_bansal UNIX for Dummies Questions & Answers 3 07-22-2013 02:41 PM
Find a string occurrence if twice in a line nnani Shell Programming and Scripting 5 07-15-2013 10:33 AM
Remove last occurrence of character (_) and rest of the string in UNIX (sed) Nallachand Shell Programming and Scripting 3 05-28-2013 04:05 AM
Find string in a file and append character brichigo Shell Programming and Scripting 9 08-21-2012 08:26 AM
find string nth occurrence in file and print line number tmalik79 Shell Programming and Scripting 6 10-10-2011 03:20 AM
remove characters from string based on occurrence of a string victor369 Shell Programming and Scripting 5 02-03-2011 09:37 PM
read a string from its end to its start and stop when you find a specific character hakermania Shell Programming and Scripting 9 09-01-2010 12:44 AM
Find the middle character from a string using sed cola Shell Programming and Scripting 6 08-23-2010 06:24 AM
Sed to print a string until the second occurrence of a character imr Shell Programming and Scripting 7 11-02-2009 06:02 AM
Find index of last occurence of a character within a string joyan321 Shell Programming and Scripting 5 06-20-2009 01:24 AM
To find a character immediately following a specified String The Observer Shell Programming and Scripting 3 07-23-2008 11:33 AM
Find and replace character in a string callimaco0082 UNIX for Dummies Questions & Answers 7 04-10-2008 11:47 AM
All times are GMT -4. The time now is 12:11 PM.

Unix & Linux Forums Content Copyright 1993-2018. All Rights Reserved.