in reply to Manipulating directory paths

Try this...

$path =~ s{ /log/ .* \z }{/}xms;

Note that if your path has "/log/" in it twice or more, this will strip off everything after the first one. If you want to strip off everything after the last one, this seems to work:

$path =~ s{ (.*) /log/ .* \z }{$1/}xms;

Replies are listed 'Best First'.
Re^2: Manipulating directory paths
by pp (Novice) on Sep 07, 2007 at 18:46 UTC
    But what if I want to replace the log/ directory and after with another directory named xyz/ like:
    CASE 1:
    Before :/home/user/phase/dir1/log/
    After: /home/user/phase/dir1/xyz/
    CASE 2:
    Before: /home/user/phase/dir1/log/Illinois/
    After:/home/user/phase/dir1/xyz/
      You can either add xyz/ to the replace part of the s/// operator, or use the concatenation operator (.).
        I tried the following: $path=~ s{ /log/ .* \z/xyz}{/}xms
        but got an error.
        I tried the options that you have suggested but it seems to work in one case and not in the other.
        I want to be able to replace everything from the log directory onwards with certain other paths as I have explained below.
        I have a conditional statement in the code where I am saying that if $ver_name is present then replace everything from the log directory onwards with $ver_name/abinitio/run into the variable graph_dir and if $ver_name is not present then replace with abinitio/run into the graph_dir.
        I have the following code:
        ( ! -z $ver_name ) ? (our $graph_dir = $before1) =~s{ /log .* \z}{/$ve +r_name/abinitio/run}xms :(our $grap h_dir=$before2) =~s{ /log/ .* \z}{abinitio/run}xms;
        CASE 1:
        my $before1 =/home/user/phase/dir1/log/
        and when passed through the conditional statement I want to derive the below graph_dir
        graph_dir= /home/user/phase/dir1/2.11/ abinitio/run

        CASE 2: my $before2 = /home/user/phase/dir1/log/Illinois/
        and when passed through the conditional statement I want to derive the below graph_dir
        graph_dir =/home/user/phase/dir1/abinitio/run/

        In my output when I see Case1 seems to work fine but when in Case 2 the value I am deriving for graph_dir is /home/user/phase/dir1//abinitio/run

        Can you please point out where I am going wrong with this?

        Thank you.