Re: using sed to search third subdirectory from $PWD

From: William Park (opengeometry_at_yahoo.ca)
Date: 12/07/04


Date: 6 Dec 2004 23:05:19 GMT

Tim Gribbin <tgribbin@buffalo.edu> wrote:
> Hello,
>
> I'm trying to use sed and $PWD to find a directory path from the third
> directory back, and the third directory name will vary. There can be
> any number of subdirectories beyond the third.
>
> For example, $PWD would produce something like this:
> /local/sandbox/[username]/project_one/project_one_1/project_one_1_1/...
>
> So I want to set a variable from sed that returns:
>
> /local/sandbox/[username]
>
> Note that the username may not necessarily be the logged in user.
>
> I'm not quite sure how to get at that third directory since I don't know
> what the value will be.
>
> I started with something like the below, but obviously this is
> inadequate since I really need to grab that third directory and back,
> but I'm not sure how to isolate the third directory. The code below
> returns all directories but I'm not sure how to limit it.
>
> set local_path = `echo $PWD | sed 's:/local/sandbox/*/:/local/sandbox/:'`
>
>
> Thanks in advance for any help!
>
> Tim Gribbin

Assuming it's always absolute path (with leading /),
    echo ... | cut -f1-4 -d/

-- 
William Park <opengeometry@yahoo.ca>
Open Geometry Consulting, Toronto, Canada
Linux solution for data processing. 


Relevant Pages

  • using sed to search third subdirectory from $PWD
    ... I'm trying to use sed and $PWD to find a directory path from the third ... I'm not quite sure how to get at that third directory since I don't know ... but I'm not sure how to isolate the third directory. ...
    (comp.unix.shell)
  • Re: Printing figure
    ... % use the function syntax of PRINT, eg, ... It worked but when I do 'pwd' I get the full directory path but ...
    (comp.soft-sys.matlab)
  • pattern matching in c-shell script
    ... In a c-shell script I put the current directory path in a variable as ... Let's say the typical $script_path ($PWD) would look something like: ... I've looked at variations of cut and haven't been able to come up with ...
    (comp.unix.shell)
  • Re: pattern matching in c-shell script
    ... Tim Gribbin wrote: ... > In a c-shell script I put the current directory path in a variable as ... Kevin Rodgers ...
    (comp.unix.shell)