Extracting a substring starting from last occurance of a string/character


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Extracting a substring starting from last occurance of a string/character
# 1  
Old 12-19-2007
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.
# 2  
Old 12-19-2007
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  
Old 12-19-2007
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
$

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Extracting substring within string between 2 token within the string

Hello. First best wishes for everybody. here is the input file ("$INPUT1") contents : BASH_FUNC_message_begin_script%%=() { local -a L_ARRAY; BASH_FUNC_message_debug%%=() { local -a L_ARRAY; BASH_FUNC_message_end_script%%=() { local -a L_ARRAY; BASH_FUNC_message_error%%=() { local... (3 Replies)
Discussion started by: jcdole
3 Replies

2. Shell Programming and Scripting

Help on extracting a substring from the input string

Hi, I am new to Unix. I am trying to extract a substring from an input string: Ex - input string: deploy_v11_9_1 i want to extract and store the value v11_9_1 from the input string in a new variable. I am using following command in my shell script file: echo "Enter the folder name u... (5 Replies)
Discussion started by: Pranav Bhasker
5 Replies

3. Shell Programming and Scripting

Extracting substring

Hi, I have string in variable like '/u/dolfin/in/DOLFIN.PRL_100.OIB.TLU.001.D20110520.T040010' and i want to conevrt this string into only "DOLFIN.PRL_100.OIB.TLU.001.D20110520.T040010" (i.e file name). Is there any command to extracting string in some part ?(rather than whole path)? ... (5 Replies)
Discussion started by: shyamu544
5 Replies

4. Shell Programming and Scripting

In Sed how can I replace starting from the 7th character to the 15th character.

Hi All, Was wondering how I can do the following.... I have a String as follows "ACCTRL000005022RRWDKKEEDKDD...." This string can be in a file called tail.out or in a Variable called $VAR2 Now I have another variable called $VAR1="000004785" (9 bytes long), I need the content of... (5 Replies)
Discussion started by: mohullah
5 Replies

5. Shell Programming and Scripting

Extracting a substring from a string in unix

Hi, I would like to extract a substring from a string in unix. eg: ./checkfile.sh -- i need only checkfile.sh from this string. Could someone help me out in this... Regards Arun (19 Replies)
Discussion started by: arunkumarmc
19 Replies

6. Shell Programming and Scripting

Extracting substring from string

Hi awk and sed gurus, Please help me in the following. I have the following entries in the file ABCDErules AbHDPrules ABCrules -- -- and other entries in the file. Now, I want to extract from the file that contain entries for *rules and process it separately. How can i do it... (6 Replies)
Discussion started by: sdosanjh
6 Replies

7. Shell Programming and Scripting

Extracting the last character of a string

How to extract the last character of a string in bash? ---------- Post updated at 03:56 PM ---------- Previous update was at 03:55 PM ---------- Suppose "abcde" is a string. i want to extract the last character i.e. "e". (1 Reply)
Discussion started by: proactiveaditya
1 Replies

8. Shell Programming and Scripting

SED: Extracting text between first occurance of foo in front of bar

Suppose I have a text file that contains the tags <foo> and <bar>. The text file can have unlimted occurances of <foo> and <bar> and looks somthing like this: <foo> Some Text <foo> Some Text <bar> Some Text <foo> Some (1 Reply)
Discussion started by: ArterialTool
1 Replies

9. Shell Programming and Scripting

shell script for extracting out the shortest substring from the given starting and en

hi all, i need an urgent help for writing a shell script which will extract out and print a substring which is the shortest substring from the given string where first and last character of that substring will be given by the user. for e.g. if str="abcdpqracdpqaserd" now if the user gives 'a'... (18 Replies)
Discussion started by: pankajd
18 Replies

10. Shell Programming and Scripting

sed, grep, awk, regex -- extracting a matched substring from a file/string

Ok, I'm stumped and can't seem to find relevant info. (I'm not even sure, I might have asked something similar before.): I'm trying to use shell scripting/UNIX commands to extract URLs from a fairly large web page, with a view to ultimately wrapping this in PHP with exec() and including the... (2 Replies)
Discussion started by: ropers
2 Replies
Login or Register to Ask a Question