![]() |
|
|
|
|
|||||||
| Forums | Portal | Register | Forum Rules | FAQ | Contribute | Members List | Arcade | Search | Today's Posts | Mark Forums Read |
| Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts here. |
|
|
||||
| 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 |
|
|
Submit Tools | LinkBack | Thread Tools | Search this Thread | Display Modes |
|
#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. |
| Forum Sponsor | ||
|
|
|
#2
|
|||
|
|||
|
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 |
|
#3
|
||||
|
||||
|
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
$
|
||||
| Google The UNIX and Linux Forums |