Find last occurrence of a character in a string


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Find last occurrence of a character in a string
# 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 or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk or other way to find out number of occurrence of 7th character

Hi all, I am looking for to filter out based on 7th character and list the number of occurrence based on the 7th character if p , d , o or m 1. if 7th character is p , Output should be: p_hosts = N 2. if 7th character is d , Output should be: d_hosts = N 3. if 7th character is o , Output... (10 Replies)
Discussion started by: rveri
10 Replies

2. Shell Programming and Scripting

To find nth position of character in string

Hi guyz i want to know nth position of character in string. For ex. var="UK,TK,HK,IND,AUS" now if we see 1st occurance of , is at 3 position, 2nd at 6,..4th at 13 position. 1st position we can find through INDEX, but what about 2nd,3rd and 4th or may be upto nth position. ? In oracle we had... (2 Replies)
Discussion started by: Jonty Immortal
2 Replies

3. Shell Programming and Scripting

Find a string occurrence if twice in a line

Hello All, I want to check if a delimiter is existing twice in a line of a text file. Suppose flat file is like this 234 | 123 123 | 345 456 | 563 | 234 | 548 So the the 3rd line has two delimiters, How can we find the lines in such a file having more then one delimiters I tried... (5 Replies)
Discussion started by: nnani
5 Replies

4. Shell Programming and Scripting

Remove last occurrence of character (_) and rest of the string in UNIX (sed)

Hi I need help on this ..!! Input : xx_abc_regA xx_def_regB xx_qwe_regC Now i required the output as the below abc def qwe Need to remove last occurrence of character (_) and rest of the string in Unix (sed). Thanks in Advance ..!!! -Nallachand (3 Replies)
Discussion started by: Nallachand
3 Replies

5. Shell Programming and Scripting

Find string in a file and append character

Hi Experts, Is there a way to find a string in a file then append a character to that string then save the file or save to another file. Here is an example. >cat test.txt NULL NULL NULL 9,800.00 NULL 1,234,567.01 I want to find all NON NULL String and add a dollar sign to those... (9 Replies)
Discussion started by: brichigo
9 Replies

6. Shell Programming and Scripting

find string nth occurrence in file and print line number

Hi I have requirement to find nth occurrence in a file and capture data from with in lines (between lines) Data in File. <QUOTE> <SESSION> <ATTRIBUTE NAME='Parameter Filename' VALUE='file1.parm'/> <ATTRIBUTE NAME='Service Name' VALUE='None'/> </SESSION> <SESSION> <ATTRIBUTE... (6 Replies)
Discussion started by: tmalik79
6 Replies

7. Shell Programming and Scripting

Find the middle character from a string using sed

Input: qwertmyuiop It should print "m". What's the command to do this with sed? (6 Replies)
Discussion started by: cola
6 Replies

8. Shell Programming and Scripting

Sed to print a string until the second occurrence of a character

Hi, I am totally new to shell scripting. I have a String "c:\working\html\index.txt.12-12-2009.bkp" I want to check if the string has more than one "." character. If it does I would like to retrieve only "c:\working\html\index.txt" i.e, discard the second occurrence of "." and the rest of the... (7 Replies)
Discussion started by: imr
7 Replies

9. Shell Programming and Scripting

To find a character immediately following a specified String

Hello, I have a UNIX file in which data is like this -- ISA*00* *00* *01*006415160 *01*137361242 ... (3 Replies)
Discussion started by: The Observer
3 Replies

10. UNIX for Dummies Questions & Answers

Find and replace character in a string

Hi all, My problem is the following: I've a script that must list all files in a directory and write this information in a text file. I've tried to get the list through ls command and then write it using msgecho msgecho "`ls $PATH_APS_JOB_ORA`" This works good but the created string... (7 Replies)
Discussion started by: callimaco0082
7 Replies
Login or Register to Ask a Question