LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 05-10-2010, 07:42 AM   #1
hcs24
LQ Newbie
 
Registered: May 2010
Posts: 2

Rep: Reputation: 0
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
 
Old 05-10-2010, 07:46 AM   #2
PMP
Member
 
Registered: Apr 2009
Location: ~
Distribution: RHEL, Fedora
Posts: 381

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

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

Last edited by PMP; 05-10-2010 at 07:49 AM. Reason: Sed version added
 
Old 05-10-2010, 07:50 AM   #3
hcs24
LQ Newbie
 
Registered: May 2010
Posts: 2

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by PMP View Post
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.
 
Old 05-10-2010, 07:51 AM   #4
PMP
Member
 
Registered: Apr 2009
Location: ~
Distribution: RHEL, Fedora
Posts: 381

Rep: Reputation: 58
You are Welcome. Please mark the thread as Solved!!
 
Old 05-10-2010, 08:45 AM   #5
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,005

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
If using bash 4+ you can use this as well in a script:
Code:
a='/vmfs/volumes/002264987352/vsma/uma.vrz'

echo ${a##*/}
 
Old 05-10-2010, 09:10 AM   #6
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,578
Blog Entries: 31

Rep: Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208
Quote:
Originally Posted by grail View Post
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
 
Old 08-18-2012, 04:54 PM   #7
kanesf
LQ Newbie
 
Registered: Jun 2012
Posts: 1

Rep: Reputation: Disabled
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
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Replace 2nd occurrence of a string in a file - sed or awk? kushalkoolwal Programming 26 09-26-2021 04:10 PM
[SOLVED] Why is char* a string and not a pointer to a char? el_b Programming 2 09-25-2009 10:33 AM
bash script that counts string occurrence tikit Programming 3 02-12-2009 11:09 AM
how to find and replace only the 2nd occurrence of similar string in a file hchoonbeng Linux - Newbie 1 10-08-2008 03:44 AM
SED replace string by occurrence uttam_h Programming 5 03-05-2008 10:02 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 10:13 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration