cutting unix path after known directoryname


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting cutting unix path after known directoryname
# 1  
Old 05-04-2010
cutting unix path after known directoryname

Hi scripters,

I know sed can do this, but I'm not to well trained to achieve this.

Problem: wherever I am in my filesystem, with a 'pwd' I can check where I am.
It may be "/usr/local/apache/etc/conf" or "/opt/apache/var/log/backup" or "/var/log/backup/apache/2009/december"

Now from these paths I want to catch the part up to "apache" and cut everyting that comes behind it, so I 'll get /usr/local/apache, /opt/apache and /var/log/backup/apache.
Can this be done?
If possible with a bit of explanation, please.

Thanks alot, Qube
# 2  
Old 05-04-2010
Code:
pwd | sed 's/apache.*/apache/'

# 3  
Old 05-04-2010
maybe with perl like this:

pwd|perl -ne'm/(.+apache)\/{0,1}/;print "$1\n"'

of course 'pwd' can replaced with anything that outputs the directory name or a list of directory names.
# 4  
Old 05-04-2010
Quote:
Originally Posted by vgersh99
Code:
pwd | sed 's/apache.*/apache/'

Quote:
Originally Posted by Qube
If possible with a bit of explanation, please.
Yes.
Assuming your current directory is /usr/local/apache/etc/conf, the sed command searches for the string apache and all the stuff behind, finds apache/etc/conf in this case and then replaces it with the string apache.
HTH
# 5  
Old 05-04-2010
A pure sh approach:

Code:
d=$PWD
case $d in
    */apache/*) d=${d%%/apache/*}/apache;;
esac

Regards,
Alister

Last edited by alister; 05-04-2010 at 06:43 PM..
# 6  
Old 05-06-2010
Thank YOU!
Afterwards things always look surprisingly easy. Smilie

Just for the sake of theory I have an additional question.
What if the path has 2 occurences of "apache" and I want to chop off from the first occurence (like in my first question)?

Thanks, Qube
# 7  
Old 05-06-2010
IMHO, the sed command from vgersh99 does already do that. Just try it.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

UNIX certification path

Hi, I'm new to Unix world, and i want to get certified in Unix because a lot of companies here in my country ask for Unix knowledge , so i'm here to ask what is the path to get such certifications? Thank you (1 Reply)
Discussion started by: thegeekaid
1 Replies

2. What is on Your Mind?

UNIX career path

Hi All, This question is regarding career path. I was not sure about which forum I should drop it, so putting it here. I have 12 years of experience on UNIX i.e. majority of Solaris and some of Linux (Suse & Red Hat). Since starting I have been working on 100% administration side and I am not... (0 Replies)
Discussion started by: solaris_1977
0 Replies

3. Shell Programming and Scripting

Setting up a path in UNIX

I have the following script "test". When i tried to execute it, I am not able to run it. I dunno why ? Then i tried getting the first few lines of the script which is displayed below: $head -10 test #!/bin/ksh PROG=$0;export PROG ORAUSER=`get_inf_env INFORM_DB_ACCOUNT`;export ORAUSER... (13 Replies)
Discussion started by: bobby1015
13 Replies

4. Programming

Unix Career path

Hi, I am having experience on Perl and C# and worked as Windows Sytem Admin and now iam planning to become a UNIX developer. I am having knowledge on basic UNIX.. can any one suggest me any good material for c/c++ UNIX programming. on what all things a UNIX Programmer needs to... (0 Replies)
Discussion started by: chandrareddy1
0 Replies

5. Shell Programming and Scripting

Creation of directoryname with Max limit

Hi, Please provide your inputs.. # define PATH_MAX 1023 /* max number of characters in a pathname (not including terminating null) */ Could you please let me know how to create directory name(or pathname) with above PATH_MAX length in korn shell scripting.. ... (9 Replies)
Discussion started by: mansa
9 Replies

6. UNIX for Dummies Questions & Answers

Cutting In Unix Question

Please Delete! Thanks. (7 Replies)
Discussion started by: Shiruken
7 Replies

7. Shell Programming and Scripting

PATH in unix

Hi, I want to know the default precedence of PATH variable (esp in HP Unix). is /usr/local/bin having higher precedence or /usr/bin? which tar is better to use /usr/local/bin/tar or /usr/bin/tar? (4 Replies)
Discussion started by: thiyagak85
4 Replies

8. UNIX for Dummies Questions & Answers

Unix Path

Hi all, I've found following information in sunfreeware: ----------------- If the package installs in /usr/local/bin, you must put /usr/local/bin (or /opt/foo/bin when programs install in /opt/fee) in your UNIX PATH. You will also probably want to add /usr/local/man to your MANPATH in a... (9 Replies)
Discussion started by: ffpradella
9 Replies

9. UNIX for Advanced & Expert Users

missing Path(in UNIX) when i launch a job on to unix machine using windows SSh

hi i want run an unix application from a windows program/application.i am using SSH(command line version)to log on to a unix machine from windows. the application has to read a configuration file inorder to run. the configuration file .CFG is in bin in my home directory. but the application... (1 Reply)
Discussion started by: megastar
1 Replies

10. UNIX for Dummies Questions & Answers

UNIX PATH info required PLEASE HELP (I'm new to unix)

I need to know how to enter a unix path in a cgi script for a guest book: example: My URL is http://www.kitachi.info I have an html file in the main folder on my site, the file is called : gbook.html what would the correct unix path for this file be ??? the part of the script... (1 Reply)
Discussion started by: akitachi
1 Replies
Login or Register to Ask a Question