The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
Google UNIX.COM


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Replacing Last occurance of & from a string vivekshady Shell Programming and Scripting 1 05-13-2008 05:15 PM
shell script for extracting out the shortest substring from the given starting and en pankajd Shell Programming and Scripting 18 03-10-2008 03:20 AM
problem extracting substring in korn shell nashrul UNIX for Dummies Questions & Answers 3 08-14-2007 11:45 PM
sed, grep, awk, regex -- extracting a matched substring from a file/string ropers Shell Programming and Scripting 2 05-23-2006 10:56 AM
Removing the last occurance of string dkhanna01 Shell Programming and Scripting 7 12-28-2004 02:46 AM

Reply
 
Submit Tools LinkBack Thread Tools Search this Thread Display Modes
  #1  
Old 12-18-2007
Registered User
 

Join Date: Dec 2007
Location: Chennai , India
Posts: 1
Extracting a substring starting from last occurance of a string/character

Hi All,
This is Ram. I'm new to this forum & new to shell scripts as well. I've a requirement in which I want to extract a substring from a given string based on last occurance of a character.

for eg.
I have a string of a file name with absolute path like
$filename=/aaa/bbb/ccc/ddd/xyz.txt

I want to extract only the actual file name ie, xyz.txt

The no. of directories ie, / will be dynamic. So I want to find the last occurance of / in the given string & to extract the string after the last /

can I have some ideas / suggestions please.

Thanks ,
Ram.
Reply With Quote
Forum Sponsor
  #2  
Old 12-18-2007
Registered User
 

Join Date: Sep 2007
Posts: 5
Ram,

You can use "basename" keyword in Unix.

for eg:

if you have a file (test1) with the content like below:

/aaa/bbb/ccc/ddd/xyz.txt
/bbb/ccc/ddd/123.txt


then your command must be like this:

for i in `cat test1`
do
File_Name=`basename $i`
echo $File_Name
done

Output:
xyz.txt
123.txt
Reply With Quote
  #3  
Old 12-19-2007
Perderabo's Avatar
Unix Daemon
 

Join Date: Aug 2001
Location: Washington DC Area
Posts: 8,667
Code:
$ pathname=/a/b/c/d/e/f/g/abc.dat
$ echo $pathname
/a/b/c/d/e/f/g/abc.dat
$ echo ${pathname##*/}
abc.dat
$
Reply With Quote
Google The UNIX and Linux Forums
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes




All times are GMT -7. The time now is 07:10 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008. All Rights Reserved.Ad Management by RedTyger Visit The Complex Event Processing Blog

Content Relevant URLs by vBSEO 3.2.0