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

Hello Monks , I am reading the following file and trying to modifay it :
if [ $YOU = 'frog' ] then export OUSER=TNN export CAR=SWs export O_PREFIX=ss export OS=corvette fi
I am reading in the file chaning O_PREFIX to a differen value what is the problem with my code here :
my $newUser ='dummy'; while(<SETORA>) { s/O_PREFIX=\w+/O_PREFIX=$newUser/ if /O_PREFIX=/; print outputfile }
thanks for any help

Replies are listed 'Best First'.
Re: REGEX with input file
by pg (Canon) on Jan 23, 2004 at 05:56 UTC

    The problem is not obvious, both duff and Roger have invetigated, and they are experienced. It has to be something else. Why don't you debug more yourself under your very own environment, with your real data. Here I gave you something to start with: (code has only been syntax checked with -c)

    my $newUser ='dummy'; while(<SETORA>) { print "read from file: [$_]\n"; if (/O_PREFIX=/) { print "regexp matched\n"; s/O_PREFIX=\w+/O_PREFIX=$newUser/; print "after s, [$_]\n"; print outputfile } }

    I don't think your data really match the regexp's you have.

Re: REGEX with input file
by duff (Parson) on Jan 23, 2004 at 04:08 UTC

    Any number of things may be wrong with your code. You haven't given us enough information to tell. You don't even say what output you are getting instead of what you want. Here are a few ideas of what could be wrong:

    • Not really a problem but the conditional is redundant. The s/// will only do its job if the LHS actually matches.
    • is outputfile really a filehandle opened for output?
    • Is SETORA really a filehandle opened for intput?
    • Are you sure you're checking the right file to see your results?
      duff , sorry if my question was not clear , ok lets put it this way what if I want to just do : my input fils has only the following
      export O_PREFIX=OLD
      I am doing the follwoing to change the value from OLD to NEW:
      my $newValue='NEW'; s/O_PREFIX=(\w+)/O_PREFIX=$newValue/ if /O_PREFIX=/;
      my code is working and I am reading from the correct file , however my output looks like this
      O_PREFIX=NEWOLD instead of O_PREFIX=NEW
      I hope this make it clear , if not then sorry :(
Re: REGEX with input file
by Roger (Parson) on Jan 23, 2004 at 05:05 UTC
    Your code should work already. I tried it and got the expected results.
    use strict; use warnings; my $newUser ='$dummy'; while (<DATA>) { s/O_PREFIX=\w+/O_PREFIX=$newUser/; # s/(?<=O_PREFIX=)\w+/$newUser/; print; } __DATA__ if [ $YOU = 'frog' ] then export OUSER=TNN export CAR=SWs export O_PREFIX=ss export OS=corvette fi

    And the output -
    if [ $YOU = 'frog' ] then export OUSER=TNN export CAR=SWs export O_PREFIX=dummy export OS=corvette fi

Re: REGEX with input file
by ysth (Canon) on Jan 23, 2004 at 09:15 UTC
    You've said the substitution is changing the output to O_PREFIX=NEWOLD where you want O_PREFIX=NEW, for some value of NEW and OLD. Perhaps whatever follows O_PREFIX doesn't actually match \w+? (Though it would have to at least start with a character matching \w, or the substitution wouldn't happen at all.)

    \w usually matches any of A-Z, a-z, 0-9, or _. If you have a larger character set you need to match, \w is not the right choice. Perhaps you could try it ass/O_PREFIX=.*/O_PREFIX=$newUser/ (which would replace from = up to the end of the line) or s/O_PREFIX=\S+/O_PREFIX=$newUser/ (which would replace from = up to a whitespace character (space, formfeed, tab, linefeed, or carriage return)).

    (What \w matches may be expanded if in the scope of use locale or matching on Unicode data.)

Re: REGEX with input file
by CountZero (Bishop) on Jan 23, 2004 at 06:57 UTC
    My guess is that your problem is not with the regex, but with the rest of your program, esp. the parts that write to your "outputfile".

    Perhaps you can show us some of that code.

    CountZero

    "If you have four groups working on a compiler, you'll get a 4-pass compiler." - Conway's Law