LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   last occurrence of a char in string (https://www.linuxquestions.org/questions/programming-9/last-occurrence-of-a-char-in-string-806943/)

hcs24 05-10-2010 07:42 AM

last occurrence of a char in string
 
Hello,

i have a couple of string paths like:
/vmfs/volumes/002264987352/vsma/uma.vrz
/vmfs/volumes/00226498882/tsda.trz
..

i want to substitude the strigs from the last occurrence of / that i have only the filenames like uma.vrz and tsda.trz.

How to do that any idea?
Thanks

PMP 05-10-2010 07:46 AM

Code:

echo '/vmfs/volumes/002264987352/vsma/uma.vrz' | awk -F"/" '{print $NF}'
or

Code:

echo '/vmfs/volumes/002264987352/vsma/uma.vrz' | sed 's#.*/##g'

hcs24 05-10-2010 07:50 AM

Quote:

Originally Posted by PMP (Post 3963267)
Code:

echo '/vmfs/volumes/002264987352/vsma/uma.vrz' | awk -F"/" '{print $NF}'
or

Code:

echo '/vmfs/volumes/002264987352/vsma/uma.vrz' | sed 's#.*/##g'



Thank you, it works perfect.

PMP 05-10-2010 07:51 AM

You are Welcome. Please mark the thread as Solved!!

grail 05-10-2010 08:45 AM

If using bash 4+ you can use this as well in a script:
Code:

a='/vmfs/volumes/002264987352/vsma/uma.vrz'

echo ${a##*/}


catkin 05-10-2010 09:10 AM

Quote:

Originally Posted by grail (Post 3963327)
If using bash 4+ you can use this as well in a script:

Works in bash 3.1 too
Code:

c@CW8:~$ bash --version
GNU bash, version 3.1.17(2)-release (i486-slackware-linux-gnu)
Copyright (C) 2005 Free Software Foundation, Inc.
c@CW8:~$ a='/vmfs/volumes/002264987352/vsma/uma.vrz'
c@CW8:~$ echo ${a##*/}
uma.vrz


kanesf 08-18-2012 04:54 PM

for filenames, try basename and dirname
 
If the question is parsing filenames to get the path and/or file name, you might want to look into dirname and basename:

$ fullname="some/long/path/to/thisfile.txt"
$ dirname $fullname
some/long/path/to
$ pathname $fullname
thisfile.txt


All times are GMT -5. The time now is 09:08 PM.