Extracting substring


Login or Register for Dates, Times and to Reply

 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Extracting substring
# 1  
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)?

In my code ,i have done some logic on this file and i moved to in_arc folder after that i wann to compress using command

compress ../in_arc/$file

it's coming error as ../in_arc//u/dolfin/in/DOLFIN.PRL_100.OIB.TLU.001.D20110520.T040010: No such file or directory

as file variable stores whole path.

Could you please advise me on this ?
# 2  
Code:
basename '/u/dolfin/in/DOLFIN.PRL_100.OIB.TLU.001.D20110520.T040010'
DOLFIN.PRL_100.OIB.TLU.001.D20110520.T040010

# 3  
Code:
file='/u/dolfin/in/DOLFIN.PRL_100.OIB.TLU.001.D20110520.T040010' 

file=${file##*/}

echo $file

# 4  
Thank you Franklin it works.
# 5  
Hi Franklin,

I am curious to know what does this '##*' mean in this regular expression?

Thanks!
# 6  
Quote:
Originally Posted by mira
Hi Franklin,

I am curious to know what does this '##*' mean in this regular expression?

Thanks!
This isn't a regex, it's a shell (kshell or bash) variable substitution syntax. The ##pattern and #pattern match the pattern at the beginning of the value contained by the variable and deletes it before the value is used. (The contents of the variable are unchanged)

The difference between the two is that ## matches the longest pattern and # matches the shortest. For example, if you had a filename /home/scooter/lib/cal.rb in variable fname and want the basename, coding ${fname##*/} matches all charcters (*) from the front of the string up to the right most slant. The result would be cal.rb

If you had coded [${fname#*/} then all characters up to the first slant would be removed and the result would have been home/scooter/lib/cal.rb.

You will also note that the "pattern" isn't a regular expression pattern, but a file globbing pattern and thus its ##*/ rather than ##.*/. This can be confusing.

The advantage to using these, as opposed to $(basename $fname), is that it is much more efficient to let the shell do the string manipulation than to invoke a separate process just to chop a string up.

Have a look at the Kshell man page as there are a lot of variable substitution tricks that can be used.
man/man1/ksh.html man page
Login or Register for Dates, Times and to Reply

Previous Thread | Next Thread
Thread Tools Search this Thread
Search this Thread:
Advanced Search

Test Your Knowledge in Computers #417
Difficulty: Medium
The terms Vanilla JavaScript and Vanilla JS refer to JavaScript which uses very small cookies during web sessions.
True or False?

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Extracting substring from variable

Hi All, I'm facing issue wherein I have 2 character string like 'CR' and 'DR' and I want to extract just 1st character but am unable to do it. I tried below options but they are returning me 2nd character only, var="CR" echo ${var:1} returns just "R" echo ${var:0} returns "CR" ... (5 Replies)
Discussion started by: arvindshukla81
5 Replies

2. Shell Programming and Scripting

Extracting a substring from Line

Hi All I have a line which is as below 1234567 Gagan Paswani zz23432 1000000000 1000000000 ASTHEYXX-RTUC zz23432 I need to extract the first occurence of zz23432 which will have the reg expression as {2}{5} Could you please assist as to how I can extract this... (3 Replies)
Discussion started by: Prashantckc
3 Replies

3. Shell Programming and Scripting

Bash - Extracting whole word containing substring

hello. I get this text when using some command : S | Name | Type | Version | Arch | Repository --+-----------------+---------+---------+------+------------- | AdobeReader_enu | package | 9.5.4-1 | i486 | zypper_local I need to get "AdobeReader_enu" from the the pattern "Ado"... (7 Replies)
Discussion started by: jcdole
7 Replies

4. 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

5. Shell Programming and Scripting

Need help in extracting the substring in UNIX

Hi all, I need to extract the "abcdef" from the below string Digital_abcdef_20130103.txt.gz The length of the "abcdef" will be changing but it will be always after the word Digital_ and before the second underscore help in this regard is highly appreciated (3 Replies)
Discussion started by: rithushri
3 Replies

6. UNIX for Dummies Questions & Answers

Extracting substring between pattern only one time

Hello ifconfig return : eth0 Link encap:Ethernet HWaddr 11:24:1D:C1:99:BA inet addr:192.168.0.1 Bcast:192.168.0.255 Mask:255.255.255.0 UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1 RX packets:37307 errors:0 dropped:0 overruns:0 frame:0 ... (2 Replies)
Discussion started by: jcdole
2 Replies

7. 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

8. 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

9. UNIX for Dummies Questions & Answers

Extracting substring using ${var:start:len}

Hi All, I am trying to extract substring from a variable by using the pattern as var3=${var2:3:3} var2 is a variable already declared and having the value. From var2 i need to extract 3 characters from 3rd position. When ever i give the expression var3=${var2:3:3} on shell prompt, the... (3 Replies)
Discussion started by: Raamc
3 Replies

10. Shell Programming and Scripting

extracting substring from a file name

hi i need to name a file with a substring of a another file name. i.e. if the old filename is abc.txt , the new filename should be abc_1.txt i should get the substring of the file name and then name the new one please let me know how to do it (4 Replies)
Discussion started by: adityamahi
4 Replies

Featured Tech Videos