Register
It is currently Wed Dec 17, 2014 4:05 pm

script to obtain


All times are UTC - 6 hours


Post new topic Reply to topic  [ 7 posts ] 
Author Message
 PostPosted: Sat Sep 11, 2010 2:05 pm   

Joined: Wed Nov 26, 2008 7:43 pm
Posts: 50
Location: Texas
Hi everyone,

How do I get the last 3 words of the path I'm currently in:
lets say I'm in

/home/john/part1/part2/part3

I want part1/part2/part3

Regards,

Ted


Top
 Profile  
 PostPosted: Sat Sep 11, 2010 2:45 pm   
User avatar

Joined: Sun Jun 27, 2010 12:57 am
Posts: 192
The Sandbox
All Scripts go here. In progress, finished, etc.


Since I see no script, I assume you wanted to post your request in the request forum?


Top
 Profile  
 PostPosted: Sat Sep 11, 2010 7:47 pm   
Site Admin
User avatar

Joined: Sun May 15, 2005 9:36 pm
Posts: 673
Location: Des Moines, Iowa
Patsie wrote:
The Sandbox
All Scripts go here. In progress, finished, etc.


Since I see no script, I assume you wanted to post your request in the request forum?



Good point... I moved it to the request forum.
Thanks Patsie.


Top
 Profile WWW  
 PostPosted: Sat Sep 11, 2010 8:04 pm   
Site Admin
User avatar

Joined: Sun May 15, 2005 9:36 pm
Posts: 673
Location: Des Moines, Iowa
It's ugly code... but hey, it's what you get when I spend a whole minute on a problem for you.
Think of it more as "food for thought" ;)
Code:
echo "/usr/local/bin/dave/test" | rev | awk -F"/" {'print $1"/"$2"/"$3'} | rev | grep -v "//"
bin/dave/test



also, look at basename.... you could do this better with basename ;)


Top
 Profile WWW  
 PostPosted: Sun Sep 12, 2010 12:51 am   
User avatar

Joined: Sun Jun 27, 2010 12:57 am
Posts: 192
I like the awk part, but feel it unnecessary to 'rev' it 2 times, so here's my solution:
Code:
echo "/home/john/part1/part2/part3" | awk -F/ '{print $(NF-2)"/"$(NF-1)"/"$NF}'


[edit]
Since 'basename' can only do the last part of a directory, I don't feel it viable for a better solution. The best I could come up with was:
Code:
var="/home/john/part1/part2/part3"
echo $(basename $(dirname $(dirname $var)))"/"$(basename $(dirname $var))"/"$(basename $var)
[/edit]


Top
 Profile  
 PostPosted: Sun Sep 12, 2010 8:34 am   
User avatar

Joined: Tue Apr 27, 2010 2:28 pm
Posts: 172
Location: Czech Republic
Pure bash:
Code:
OLDIFS=$IFS
IFS='/'
steps=($PWD)
echo "${steps[*]: -3:3}"
IFS=$OLDIFS
:-B
You have not specified what should happen if the path has less than three components, though.


Top
 Profile  
 PostPosted: Mon Sep 27, 2010 9:30 pm   
Site Admin
User avatar

Joined: Sun May 15, 2005 9:36 pm
Posts: 673
Location: Des Moines, Iowa
Patsie wrote:
I like the awk part, but feel it unnecessary to 'rev' it 2 times, so here's my solution:



he he he , I said it was UGLY code ;)


Top
 Profile WWW  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 7 posts ] 

All times are UTC - 6 hours


Who is online

Users browsing this forum: Google [Bot], Yahoo [Bot] and 1 guest


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  


BashScripts | Promote Your Page Too
Powered by phpBB © 2011 phpBB Group
© 2003 - 2011 USA LINUX USERS GROUP