Unix/Linux Go Back    


Shell Programming and Scripting Unix shell scripting - KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and 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 Linux

Last edited by zaxxon; 07-10-2013 at 04: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: 4 September 2015, 8:47 AM EDT
Location: St. Gallen, Switzerland
Posts: 6,318
Thanks: 137
Thanked 470 Times in 424 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: 17 April 2015, 12:45 AM EDT
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: 4 September 2015, 12:38 PM EDT
Location: Zürich
Posts: 8,576
Thanks: 337
Thanked 1,080 Times in 938 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

Linux 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 07: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 07:23 AM
cut file details from the path given harshada UNIX for Dummies Questions & Answers 2 10-01-2008 07:08 AM
Trying to cut a path to a file dkieran Shell Programming and Scripting 14 05-20-2007 02:59 PM



All times are GMT -4. The time now is 08:18 AM.