Noame has asked for the wisdom of the Perl Monks concerning the following question:


Hi,

I've unix 'INCLUDE' environment variable that initialize as shown below:
/usr/include:/users/ctbld/246_EMEA_Sched/BG/INC:/users/ctbld/246_EMEA_ +Sched/BG/INC/new_infrastructure:/users/ctbld/246_EMEA_Sched/BG/INC/Co +rba: /users/ctbld/246_EMEA_Sched/BG/Extmodul::/users/ctbld/246_EMEA_Sched/B +G/Extmodul/imageMagic/include::/usr/local/mainwin/userx/public/sdk/in +c:/opt2/oracle10.2/rdbms/public::/opt2/oracle10.2/rdbms/demo:/usr/loc +al/mainwin/mfc400/src:/opt/iona/asp/5.1/include:/opt/bea/tuxedo8.1/in +clude:/opt/mqm/inc::/opt/sna/include:/users/ctbld/246_EMEA_Sched/BG/E +xtmodul/Sun/X25/include::/opt/SUNWspro/prod/include/CC/std

I want to initialize new environment that include only the path that started with "/users/ctbld/" and to remove the other path.

An example, after the manipulation the new Variable should to include only:
NEW_INCLUDE=/users/ctbld/246_EMEA_Sched/BG/INC:/users/ctbld/246_EMEA_S +ched/BG/INC/new_infrastructure: /users/ctbld/246_EMEA_Sched/BG/INC/Corba:/users/ctbld/246_EMEA_Sched/B +G/Extmodul::/users/ctbld/246_EMEA_Sched/BG/Extmodul/imageMagic/includ +e

The Delimiter between the path is ':'

In which command can I get only the relevant PATH (as mention above) in variable (not in array).
Thanks.

Replies are listed 'Best First'.
Re: string manipulation
by FunkyMonk (Bishop) on May 18, 2008 at 11:54 UTC
    • get the include paths from $ENV{INCLUDE}
    • split on /:/
    • grep for the parts you want
    • join the parts back together again:

    print join ":", grep { m{^/users/ctbld/} } split /:/, $ENV{INCLUDE};

    However, you'll have a hard time manipulating environment variables from within a Perl program, so you'll have to do that as a one-liner

    Update:

    Damn it, its INCLUDE, not PATH


    Unless I state otherwise, all my code runs with strict and warnings
Re: string manipulation
by BrowserUk (Patriarch) on May 18, 2008 at 11:51 UTC

      Thanks.

      Another question in the same issue: I have the following initialization
      CPP_PROJ=/nologo /MDd /W3 /Gm /GX /ZI /Od /D "_DEBUG" /D "_LIB" /D "_F +P_BKLIB" /D "_MBCS" /D "FTLIB" /D "_AFXDLL" /D "WIN32" /FR"$(I NTDIR)\\" /Fp"$(INTDIR)\ApplicationObject.pch" /YX /Fo"$(INTDIR)\\" /F +d"$(INTDIR)\\" /FD /GZ /c

      I need to match all the strings start with /D "_<any_string" to variable and replace "/D" with "-D" so finally the new variable should to be like:
      $NEW_VAR = "-D_ DEBUG -D_LIB -D_FP_BKLIB -D_MBCS - DFTLIB …"
      Please advice. Thanks again
        (my @d) = $str =~ m!/D\s+"([^"]+)"!g; my $var = join " ", map{"-D$_"}@d;