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
shell variables and sed substitution duderonomy UNIX for Advanced & Expert Users 2 05-04-2008 11:54 PM
how to pass variables surrounded in double quotes to awk? cruiser AIX 4 03-24-2006 01:12 PM
double variable substitution asal_email2 UNIX for Dummies Questions & Answers 1 06-13-2005 09:35 PM
ksh substitution solea Shell Programming and Scripting 2 08-09-2004 02:30 AM
substitution supercbw Shell Programming and Scripting 1 06-07-2002 12:07 PM

Reply
 
Submit Tools LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 05-16-2007
Registered User
 

Join Date: Aug 2001
Posts: 58
Double Substitution variables in ksh

Hi

I have a variable whose value is like this
i=/test/test1/test2/myfile.cd.070505123457
i would like to have the value of myfile.cd stored into another variable

my attempt is
test=${i##*/} ;echo $test ##and i get
myfile.cd.070505123457

since what i wnat is myfile.cd i try this
test={{${i##*/}}##.} ;echo $test
and i get an error

Any idea how do i get what i want ?

regards
xiamin
Reply With Quote
Forum Sponsor
  #2 (permalink)  
Old 05-16-2007
Registered User
 

Join Date: Mar 2006
Location: Bangalore,India
Posts: 1,397
Code:
i=/test/test1/test2/myfile.cd.070505123457
test=${i##*/}
test=${test%.*}
Reply With Quote
  #3 (permalink)  
Old 05-16-2007
Registered User
 

Join Date: Aug 2001
Posts: 58
Hi Anbu

Thanks for your reply
I was wundering if we could do that in one line

regards
xiamin
Reply With Quote
  #4 (permalink)  
Old 05-16-2007
Ygor's Avatar
Moderator
 

Join Date: Oct 2003
Location: -31.96,115.84
Posts: 1,243
Try...
Code:
test=$(expr $i : '.*/\(.*\)\..*')
Reply With Quote
  #5 (permalink)  
Old 05-16-2007
Registered User
 

Join Date: Aug 2001
Posts: 58
Hi Ygor

Your solution works like charm..

But now i am pulling my hair out trying to understand what magic your code tries to do...

Can you please explain or else i might go bald :-)

regards
Hrishy
Reply With Quote
  #6 (permalink)  
Old 05-16-2007
Perderabo's Avatar
Unix Daemon
 

Join Date: Aug 2001
Location: Washington DC Area
Posts: 8,466
Quote:
Originally Posted by xiamin
I was wundering if we could do that in one line
Code:
i=/test/test1/test2/myfile.cd.070505123457 ; test=${i##*/} ; test=${test%.*}
Sorry, I couldn't resist...
Reply With Quote
  #7 (permalink)  
Old 05-16-2007
Registered User
 

Join Date: Aug 2001
Posts: 58
Hi Perderabo

Your welcome :-)

Sometimes we are tempted and take the bait ;-).I guess you are seduced by unix

regards
Hrishy
Reply With Quote
Google The UNIX and Linux Forums
Reply

Tags
regex, regular expressions

Thread Tools
Display Modes




All times are GMT -7. The time now is 01:15 PM.


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 Global Fact Book

Content Relevant URLs by vBSEO 3.2.0