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    
 
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:
Cut the path into two parts 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: 24 August 2016, 10:55 AM EDT
Location: St. Gallen, Switzerland
Posts: 6,461
Thanks: 155
Thanked 518 Times in 458 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: 23 March 2016, 5:15 AM EDT
Location: INDIA, Bangalore
Posts: 2,050
Thanks: 16
Thanked 107 Times in 103 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: 24 August 2016, 1:39 AM EDT
Location: Zürich
Posts: 8,595
Thanks: 342
Thanked 1,092 Times in 948 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

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



All times are GMT -4. The time now is 07:19 PM.