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

  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 Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 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

Last edited by zaxxon; 07-10-2013 at 04:06 AM.. Reason: code tags
Sponsored Links
    #2  
Old 07-10-2013
zaxxon's Avatar
zaxxon zaxxon is offline Forum Staff  
code tag tagger
 
Join Date: Sep 2007
Last Activity: 23 September 2014, 7:56 AM EDT
Location: St. Gallen, Switzerland
Posts: 6,229
Thanks: 121
Thanked 451 Times in 411 Posts

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

Sponsored Links
    #3  
Old 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 07-10-2013
vidyadhar85's Avatar
vidyadhar85 vidyadhar85 is offline Forum Advisor  
The Tutor
 
Join Date: Jun 2008
Last Activity: 9 April 2014, 1:31 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 07-10-2013
Scott's Avatar
Scott Scott is offline Forum Staff  
Administrator
 
Join Date: Jun 2009
Last Activity: 23 September 2014, 5:13 PM EDT
Location: Zürich
Posts: 8,483
Thanks: 332
Thanked 1,047 Times in 914 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

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

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 06:25 PM.