Cut the path into two parts | Unix Linux Forums | Shell Programming and Scripting

  Unix/Linux Go Back    


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

Cut the path into two parts

Shell Programming and Scripting


Closed Linux or Unix Question    
 
Thread Tools Search this Thread Display Modes
    #1  
Old Unix and Linux 07-10-2013
munna_dude munna_dude is offline
Registered User
 
Join Date: Jan 2007
Last Activity: 19 July 2013, 3:37 AM EDT
Posts: 50
Thanks: 0
Thanked 0 Times in 0 Posts
Cut the path into two parts

Hi,


Code:
file=/usr/lib

I need to cut and put it into two variable like


Code:
string1=/usr
string2=lib

I made it for string2

Code:
string2=${file#/*/}

How to get String1 in the same way which I have get string2.

Moderator's Comments:
Use even more code tags Unix or Linux Image

Last edited by zaxxon; 07-10-2013 at 05:06 AM.. Reason: code tags
Sponsored Links
    #2  
Old Unix and Linux 07-10-2013
zaxxon's Unix or Linux Image
zaxxon zaxxon is offline Forum Staff  
code tag tagger
 
Join Date: Sep 2007
Last Activity: 6 March 2015, 2:29 AM EST
Location: St. Gallen, Switzerland
Posts: 6,256
Thanks: 128
Thanked 455 Times in 414 Posts

Code:
$ echo ${file%/*}
/usr

Sponsored Links
    #3  
Old Unix and Linux 07-10-2013
munna_dude munna_dude is offline
Registered User
 
Join Date: Jan 2007
Last Activity: 19 July 2013, 3:37 AM EDT
Posts: 50
Thanks: 0
Thanked 0 Times in 0 Posts
Quote:
Originally Posted by zaxxon View Post
Code:
$ echo ${file%/*}
/usr

Thanks. it is working
    #4  
Old Unix and Linux 07-10-2013
vidyadhar85's Unix or Linux Image
vidyadhar85 vidyadhar85 is offline Forum Advisor  
The Tutor
 
Join Date: Jun 2008
Last Activity: 27 February 2015, 3:41 AM EST
Location: INDIA, Bangalore
Posts: 2,049
Thanks: 16
Thanked 106 Times in 102 Posts

Code:
 
dirname /usr/lib

will also work out for you
Sponsored Links
    #5  
Old Unix and Linux 07-10-2013
Scott's Unix or Linux Image
Scott Scott is offline Forum Staff  
Administrator
 
Join Date: Jun 2009
Last Activity: 6 March 2015, 4:53 AM EST
Location: Zürich
Posts: 8,530
Thanks: 336
Thanked 1,065 Times in 928 Posts
I suspect your other one is wrong, too - it stops working when the path > two deep.


Code:
string1=${file#/*/}

Should be

Code:
string1=${file##*/}


Code:
$ file=/a/b/c
$ echo ${file#/*/}
b/c
$ echo ${file##*/}
c

The Following User Says Thank You to Scott For This Useful Post:
RavinderSingh13 (07-10-2013)
Sponsored Links
Closed Linux or Unix Question

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Unix or Linux Image More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Extracting parts from an absolute path mortanon UNIX for Dummies Questions & Answers 9 06-15-2010 08:47 AM
How to cut a string in two parts and show the other part adityamitra UNIX for Dummies Questions & Answers 6 05-27-2010 08:23 AM
cut file details from the path given harshada UNIX for Dummies Questions & Answers 2 10-01-2008 08:08 AM
Trying to cut a path to a file dkieran Shell Programming and Scripting 14 05-20-2007 03:59 PM



All times are GMT -4. The time now is 12:00 PM.