UNIX Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Try It Free
Go Back   Dev Shed ForumsOperating SystemsUNIX Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
Stop making mediocre tutorials.The best tutorials are video! Camtasia Studio makes it easy to create engaging, buzz-building screen videos at any size, in any popular format. Download the free trial!
  #1  
Old March 31st, 2005, 11:32 AM
mikie7boy mikie7boy is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Apr 2003
Posts: 97 mikie7boy User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 10 h 1 m 4 sec
Reputation Power: 6
search a string for a character

Hello people,

I have variable that contains a path address of sorts, as seen below. What i need to do, if possible, is to search the string from the end to find the last '/' character and then delete everything that comes after the /

So i would start with:

MY_PATH=./tools/calculator

and then finish up with:

MY_PATH=./tools/

How could this be done?

Cheers Mike

Reply With Quote
  #2  
Old March 31st, 2005, 06:52 PM
guggach guggach is offline
Contributing User
Dev Shed Beginner (1000 - 1499 posts)
 
Join Date: Jul 2004
Location: Middle Europa
Posts: 1,083 guggach User rank is Corporal (100 - 500 Reputation Level)guggach User rank is Corporal (100 - 500 Reputation Level)guggach User rank is Corporal (100 - 500 Reputation Level)guggach User rank is Corporal (100 - 500 Reputation Level) 
Time spent in forums: 4 Days 19 h 44 m 45 sec
Reputation Power: 9
man basename
and
man dirname
__________________
working on Solaris[5-9], preferred languages french and C.

Reply With Quote
  #3  
Old April 1st, 2005, 04:59 AM
mikie7boy mikie7boy is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Apr 2003
Posts: 97 mikie7boy User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 10 h 1 m 4 sec
Reputation Power: 6
Ok cheers mate i've got it working now

Thanks for pointing me in the right direction

Mike...

Reply With Quote
  #4  
Old April 1st, 2005, 05:51 AM
zlutovsky zlutovsky is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2004
Location: Prague, Czech Rep.
Posts: 116 zlutovsky User rank is Corporal (100 - 500 Reputation Level)zlutovsky User rank is Corporal (100 - 500 Reputation Level)zlutovsky User rank is Corporal (100 - 500 Reputation Level)zlutovsky User rank is Corporal (100 - 500 Reputation Level) 
Time spent in forums: 22 h 34 sec
Reputation Power: 6
Quote:
Originally Posted by mikie7boy
Ok cheers mate i've got it working now

Thanks for pointing me in the right direction

Mike...



Hi,

very useful but very rarely used technique is the notation

${variable#pattern}
${variable##pattern}
${variable%pattern}
${variable%%pattern}

The pattern can be anything legal in shell script, i. e. variable reference or output of a command etc. So this method is not limited to the patterns starting with a slash. Try this:

:
A=X/aaa/bbb/ccc
echo 1. ${A%/*}
echo 2. ${A%%/*}
echo 3. ${A#X*/}
echo 4. ${A##X*/}

Have a fun

Reply With Quote
  #5  
Old April 6th, 2005, 06:24 AM
guggach guggach is offline
Contributing User
Dev Shed Beginner (1000 - 1499 posts)
 
Join Date: Jul 2004
Location: Middle Europa
Posts: 1,083 guggach User rank is Corporal (100 - 500 Reputation Level)guggach User rank is Corporal (100 - 500 Reputation Level)guggach User rank is Corporal (100 - 500 Reputation Level)guggach User rank is Corporal (100 - 500 Reputation Level) 
Time spent in forums: 4 Days 19 h 44 m 45 sec
Reputation Power: 9
do not insist

Quote:
Originally Posted by zlutovsky
Hi,

very useful but very rarely used technique is the notation

${variable#pattern}
${variable##pattern}
${variable%pattern}
${variable%%pattern}

The pattern can be anything legal in shell script, i. e. variable reference or output of a command etc. So this method is not limited to the patterns starting with a slash. Try this:

:
A=X/aaa/bbb/ccc
echo 1. ${A%/*}
echo 2. ${A%%/*}
echo 3. ${A#X*/}
echo 4. ${A##X*/}

Have a fun


you still insist a well known shell is running
try to be a little more 'portable'

Reply With Quote
  #6  
Old April 7th, 2005, 03:49 AM
zlutovsky zlutovsky is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2004
Location: Prague, Czech Rep.
Posts: 116 zlutovsky User rank is Corporal (100 - 500 Reputation Level)zlutovsky User rank is Corporal (100 - 500 Reputation Level)zlutovsky User rank is Corporal (100 - 500 Reputation Level)zlutovsky User rank is Corporal (100 - 500 Reputation Level) 
Time spent in forums: 22 h 34 sec
Reputation Power: 6
Quote:
Originally Posted by guggach
you still insist a well known shell is running
try to be a little more 'portable'



Well yes guggach,

I am trying to help people even if they do not specify their interpreter. So I usualy specify the interpreter I am using. This time I have forgotten to do so. Can you forgive it to me?

My present script was tested under ksh or bash on AIX, HP-UX, Linux and SCO. Are you satisfied with it?

And what YOU are doing for the people who need help?

Regards

Reply With Quote
Reply

Viewing: Dev Shed ForumsOperating SystemsUNIX Help > search a string for a character


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 4 hosted by Hostway